######################### Load Libraries #######################################

library(foreign)

library(seatsvotes)

############################ Data Setup ########################################

########################
###### Australia #######
########################

# Documentation: aul_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/aul")

# generate vector of election years
aul_yrs <- c(1946,1949,1951,1954,1955,1958,1961,1963,1966,1969,1972,1974,1975,1977,1980,1983,1984,1987,1990,1993,1996,1998,2001,2004)

# generate empty list with length equal to the number of election years
AUL.elections <- vector("list",length(aul_yrs))

# use election years as names for items of list 
names(AUL.elections) <- sprintf("%04.0f",aul_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in aul_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	AUL.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}

# use districts as row names; eliminate unnecessary columns; rename first column 
for (i in 1:length(AUL.elections)) {

	rownames(AUL.elections[[i]]) <- AUL.elections[[i]]$district_name
	
	AUL.elections[[i]] <- AUL.elections[[i]][-c(1,2,4,5,6)]
	
	names(AUL.elections[[i]])[1] <- "Total"	
}


#####################
###### Canada #######
#####################

# Documentation: can_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/can")

# generate vector of election years
can_yrs <- c(1945,1949,1953,1957,1958,1962,1963,1965,1968,1972,1974,1979,1980,1984,1988,1993,1997,2000,2004,2006,2008)

# generate empty list with length equal to the number of election years
CAN.elections <- vector("list",length(can_yrs))

# use election years as names for items of list 
names(CAN.elections) <- sprintf("%04.0f",can_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in can_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	CAN.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}

# use districts as row names; eliminate unnecessary columns; rename first column
for (i in 1:length(CAN.elections)) {

	rownames(CAN.elections[[i]]) <- paste(CAN.elections[[i]]$district_name,CAN.elections[[i]]$province, sep=",")
	
	CAN.elections[[i]] <- CAN.elections[[i]][-c(1,2,3,5,6,7)]
	
	names(CAN.elections[[i]])[1] <- "Total"	
}


##################
##### France #####
##################

# Documentation: fra_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/fra")

# generate vector of election years
fra_yrs <- c(1973,1978,1981,1986,1988,1993,1997,2002)

# generate empty list with length equal to the number of election years
FRA.elections <- vector("list",length(fra_yrs))

# use election years as names for items of list
names(FRA.elections) <- sprintf("%04.0f",fra_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in fra_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	FRA.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}


# use districts as row names; eliminate unnecessary columns; rename first column
for (i in c(1,3,5:8)) {
# NOTE: The 1986 election was held using party-list PR, so it is dropped from this analysis.

	rownames(FRA.elections[[i]]) <- FRA.elections[[i]]$district_name
	
	FRA.elections[[i]] <- FRA.elections[[i]][,c(1:4,6:ncol(FRA.elections[[i]]),5)]
	
	FRA.elections[[i]] <- FRA.elections[[i]][-c(1:(which(names(FRA.elections[[i]])=="total_votes2")-1))]
	
# 	names(FRA.elections[[i]])[1] <- "Total"	

}


#######################
##### New Zealand #####
#######################

# Documentation: nz_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/nz")

# generate vector of election years
nz_yrs <- c(1946,1949,1951,1954,1957,1960,1963,1966,1969,1972,1975,1978,1981,1984,1987,1990,1996,1999)

# generate empty list with length equal to the number of election years
NZ.elections <- vector("list",length(nz_yrs))

# use election years as names for items of list
names(NZ.elections) <- sprintf("%04.0f",nz_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in nz_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	NZ.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}

# use districts as row names; eliminate unnecessary columns; rename first column
for (i in 1:length(NZ.elections)) {

	rownames(NZ.elections[[i]]) <- NZ.elections[[i]]$district_name
	
	NZ.elections[[i]] <- NZ.elections[[i]][-c(1,2)]
	
	names(NZ.elections[[i]])[1] <- "Total"	
}


##########################
##### United Kingdom #####
##########################

# Documentation: uk_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/uk")

# generate vector of election years
uk_yrs <- c(1945,1950,1951,1955,1959,1964,1966,1970,19741,19742,1979,1983,1987,1992,1997,2001,2005)

# generate empty list with length equal to the number of election years
UK.elections <- vector("list",length(uk_yrs))

# use election years as names for items of list
names(UK.elections) <- sprintf("%04.0f",uk_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in uk_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	UK.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}

# district "Central" appears twice in some elections; add new level "Central2" and change one of the duplicate district names to "Central2" 
levels(UK.elections[[9]]$district_name) <- c(levels(UK.elections[[9]]$district_name),"Central2")
UK.elections[[9]]$district_name[126] <- "Central2"

# district "Central" appears twice in some elections; add new level "Central2" and change one of the duplicate district names to "Central2" 
levels(UK.elections[[10]]$district_name) <- c(levels(UK.elections[[10]]$district_name),"Central2")
UK.elections[[10]]$district_name[126] <- "Central2"

# district "Central" appears twice in some elections; add new level "Central2" and change one of the duplicate district names to "Central2" 
levels(UK.elections[[14]]$district_name) <- c(levels(UK.elections[[14]]$district_name),"Central2")
UK.elections[[14]]$district_name[119] <- "Central2"

# use districts as row names; eliminate unnecessary columns; rename first column
for (i in 1:length(UK.elections)) {

	rownames(UK.elections[[i]]) <- UK.elections[[i]]$district_name
	
	UK.elections[[i]] <- UK.elections[[i]][-c(1,2,4)]
	
	names(UK.elections[[i]])[1] <- "Total"	
}


#########################
##### United States #####
#########################

# Documentation: us_elections.xls

# change to directory with relevant electoral data
setwd("~/_data/electoral/us")

# generate vector of election years
us_yrs <- seq(1946, 2006, by=2)

# generate empty list with length equal to the number of election years
US.elections <- vector("list",length(us_yrs))

# use election years as names for items of list
names(US.elections) <- sprintf("%04.0f",us_yrs)

# read csv files for election years into R and assign the data matrices to respective items of the elections list
for (i in us_yrs) {

	name <- paste(i, "election", sep = "_")
	
	yr <- paste(i,sep ="")

	US.elections[[paste(yr)]] <- read.csv(paste(name,"csv", sep="."), sep=";")
	
}

# use districts as row names; eliminate unnecessary columns; rename first column
for (i in 1:length(US.elections)) {

	US.elections[[i]] <- US.elections[[i]][US.elections[[i]][,5]==0,]

	rownames(US.elections[[i]]) <- US.elections[[i]]$district_name
	
	US.elections[[i]] <- US.elections[[i]][-c(1:3,5:7)]
	
	names(US.elections[[i]])[1] <- "Total"	
}


############################ Analysis ##########################################

# Steps (applies to all countries):
# 1. Generate data frame for relevant election year.
# 2. Find contestation patterns (seatsvotes packages).
# 3. Estimate mixture model based on contestation patterns identified in Step 2 (seatsvotes package).
# 4. Verify fit of mixture model estimated in Step 3 (seatsvotes package).
# 5. Simulate seats-votes elasticities (seatsvotes package).


########################
###### Australia #######
########################

# Source: http://en.wikipedia.org/wiki/Australian_federal_election,_1961 (and other years)

# set random number seed for replicability
set.seed(1234)

##########
## 1946 ##
##########

# NOTE: Liberal Country Party (VIC) won one seat in 1946, but is not included here.

# Step 1
aul46 <- AUL.elections$'1946'
aul46 <- data.frame(Total=aul46$Total,
                   Labor=aul46$vsh_lab,
                   Liberal=aul46$vsh_lib_pty,
                   Country=aul46$vsh_cntry)

# Step 2
aul46.split <- findpatterns(aul46)

# Step 3
fit46 <- list()
fit46[[1]] <- mvnmix(dat=aul46.split[[4]],components=1,nrep=10,scatter=T)
fit46[[2]] <- mvnmix(dat=aul46.split[[5]],components=3,nrep=10,scatter=T)
fit46[[3]] <- mvnmix(dat=aul46.split[[6]],components=1,nrep=10,scatter=T)

# Step 4
show.marginals(fit46,numdraws=50000)

# Step 5
sr46 <- swingratio(fit46,sims=10000)

##########
## 1949 ##
##########

aul49 <- AUL.elections$'1949'
aul49 <- data.frame(Total=aul49$Total,
                   Labor=aul49$vsh_lab,
                   Liberal=aul49$vsh_lib_pty,
                   Country=aul49$vsh_cntry)

aul49.split <- findpatterns(aul49)

fit49 <- list()
fit49[[1]] <- mvnmix(dat=aul49.split[[4]],components=1,nrep=10,scatter=T)
fit49[[2]] <- mvnmix(dat=aul49.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit49,numdraws=50000)

sr49 <- swingratio(fit49,sims=10000)

##########
## 1951 ##
##########

aul51 <- AUL.elections$'1951'
aul51 <- data.frame(Total=aul51$Total,
                   Labor=aul51$vsh_lab,
                   Liberal=aul51$vsh_lib_pty,
                   Country=aul51$vsh_cntry)

aul51.split <- findpatterns(aul51)

fit51 <- list()
fit51[[1]] <- mvnmix(dat=aul51.split[[3]],components=3,nrep=10,scatter=T)
fit51[[2]] <- mvnmix(dat=aul51.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit51,numdraws=50000)

sr51 <- swingratio(fit51,sims=10000)

##########
## 1954 ##
##########

aul54 <- AUL.elections$'1954'
aul54 <- data.frame(Total=aul54$Total,
                   Labor=aul54$vsh_lab,
                   Liberal=aul54$vsh_lib_pty,
                   Country=aul54$vsh_cntry)

aul54.split <- findpatterns(aul54)

fit54 <- list()
fit54[[1]] <- mvnmix(dat=aul54.split[[4]],components=3,nrep=10,scatter=T)
fit54[[2]] <- mvnmix(dat=aul54.split[[5]],components=3,nrep=10,scatter=T)

show.marginals(fit54,numdraws=50000)

sr54 <- swingratio(fit54,sims=10000)

##########
## 1955 ##
##########

aul55 <- AUL.elections$'1955'
aul55 <- data.frame(Total=aul55$Total,
                   Labor=aul55$vsh_lab,
                   Liberal=aul55$vsh_lib_pty,
                   Country=aul55$vsh_cntry)

aul55.split <- findpatterns(aul55)

fit55 <- list()
fit55[[1]] <- mvnmix(dat=aul55.split[[4]],components=3,nrep=10,scatter=T)
fit55[[2]] <- mvnmix(dat=aul55.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit55,numdraws=50000)

sr55 <- swingratio(fit55,sims=10000)

##########
## 1958 ##
##########

aul58 <- AUL.elections$'1958'
aul58 <- data.frame(Total=aul58$Total,
                   Labor=aul58$vsh_lab,
                   Liberal=aul58$vsh_lib_pty,
                   Country=aul58$vsh_cntry)

aul58.split <- findpatterns(aul58)

fit58 <- list()
fit58[[1]] <- mvnmix(dat=aul58.split[[2]],components=1,nrep=10,scatter=T)
fit58[[2]] <- mvnmix(dat=aul58.split[[4]],components=3,nrep=10,scatter=T)
fit58[[3]] <- mvnmix(dat=aul58.split[[5]],components=3,nrep=10,scatter=T)

show.marginals(fit58,numdraws=50000)

sr58 <- swingratio(fit58,sims=10000)

##########
## 1961 ##
##########

aul61 <- AUL.elections$'1961'
aul61 <- data.frame(Total=aul61$Total,
                   Labor=aul61$vsh_lab,
                   Liberal=aul61$vsh_lib_pty,
                   Country=aul61$vsh_cntry)

aul61.split <- findpatterns(aul61)

fit61 <- list()
fit61[[1]] <- mvnmix(dat=aul61.split[[3]],components=2,nrep=10,scatter=T)
fit61[[2]] <- mvnmix(dat=aul61.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit61,numdraws=50000)

sr61 <- swingratio(fit61,sims=10000)

##########
## 1963 ##
##########

aul63 <- AUL.elections$'1963'
aul63 <- data.frame(Total=aul63$Total,
                   Labor=aul63$vsh_lab,
                   Liberal=aul63$vsh_lib_pty,
                   Country=aul63$vsh_cntry)

aul63.split <- findpatterns(aul63)

fit63 <- list()
fit63[[1]] <- mvnmix(dat=aul63.split[[4]],components=2,nrep=10,scatter=T)
fit63[[2]] <- mvnmix(dat=aul63.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit63,numdraws=50000)

sr63 <- swingratio(fit63,sims=10000)

##########
## 1966 ##
##########

aul66 <- AUL.elections$'1966'
aul66 <- data.frame(Total=aul66$Total,
                   Labor=aul66$vsh_lab,
                   Liberal=aul66$vsh_lib_pty,
                   Country=aul66$vsh_cntry)

aul66.split <- findpatterns(aul66)

fit66 <- list()
fit66[[1]] <- mvnmix(dat=aul66.split[[3]],components=3,nrep=10,scatter=T)
fit66[[2]] <- mvnmix(dat=aul66.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit66,numdraws=50000)

sr66 <- swingratio(fit66,sims=10000)

##########
## 1969 ##
##########

aul69 <- AUL.elections$'1969'
aul69 <- data.frame(Total=aul69$Total,
                   Labor=aul69$vsh_lab,
                   Liberal=aul69$vsh_lib_pty,
                   Country=aul69$vsh_cntry)

aul69.split <- findpatterns(aul69)

fit69 <- list()
fit69[[1]] <- mvnmix(dat=aul69.split[[2]],components=3,nrep=10,scatter=T)
fit69[[2]] <- mvnmix(dat=aul69.split[[3]],components=2,nrep=10,scatter=T)

show.marginals(fit69,numdraws=50000)

sr69 <- swingratio(fit69,sims=10000)

##########
## 1972 ##
##########

aul72 <- AUL.elections$'1972'
aul72 <- data.frame(Total=aul72$Total,
                   Labor=aul72$vsh_lab,
                   Liberal=aul72$vsh_lib_pty,
                   Country=aul72$vsh_cntry)

aul72.split <- findpatterns(aul72)

fit72 <- list()
fit72[[1]] <- mvnmix(dat=aul72.split[[1]],components=2,nrep=10,scatter=T)
fit72[[2]] <- mvnmix(dat=aul72.split[[2]],components=1,nrep=10,scatter=T)
fit72[[3]] <- mvnmix(dat=aul72.split[[3]],components=2,nrep=10,scatter=T)

show.marginals(fit72,numdraws=50000)

sr72 <- swingratio(fit72,sims=10000)

##########
## 1974 ##
##########

aul74 <- AUL.elections$'1974'
aul74 <- data.frame(Total=aul74$Total,
                   Labor=aul74$vsh_lab,
                   Liberal=aul74$vsh_lib_pty,
                   Country=aul74$vsh_cntry)

aul74.split <- findpatterns(aul74)

fit74 <- list()
fit74[[1]] <- mvnmix(dat=aul74.split[[2]],components=3,nrep=10,scatter=T)
fit74[[2]] <- mvnmix(dat=aul74.split[[3]],components=2,nrep=10,scatter=T)
fit74[[3]] <- mvnmix(dat=aul74.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit74,numdraws=50000)

sr74 <- swingratio(fit74,sims=10000)

##########
## 1975 ##
##########

aul75 <- AUL.elections$'1975'
aul75 <- data.frame(Total=aul75$Total,
                   Labor=aul75$vsh_lab,
                   Liberal=aul75$vsh_lib_pty,
                   NationalCountry=aul75$vsh_nat_cntry)

aul75.split <- findpatterns(aul75)

fit75 <- list()
fit75[[1]] <- mvnmix(dat=aul75.split[[2]],components=3,nrep=10,scatter=T)
fit75[[2]] <- mvnmix(dat=aul75.split[[3]],components=3,nrep=10,scatter=T)
fit75[[3]] <- mvnmix(dat=aul75.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit75,numdraws=50000)

sr75 <- swingratio(fit75,sims=10000)

##########
## 1977 ##
##########

aul77 <- AUL.elections$'1977'
aul77 <- data.frame(Total=aul77$Total,
                   Labor=aul77$vsh_lab,
                   Liberal=aul77$vsh_lib_pty,
                   NationalCountry=aul77$vsh_nat_cntry)

aul77.split <- findpatterns(aul77)

fit77 <- list()
fit77[[1]] <- mvnmix(dat=aul77.split[[3]],components=2,nrep=10,scatter=T)
fit77[[2]] <- mvnmix(dat=aul77.split[[4]],components=2,nrep=10,scatter=T)
fit77[[3]] <- mvnmix(dat=aul77.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit77,numdraws=50000)

sr77 <- swingratio(fit77,sims=10000)

##########
## 1980 ##
##########

aul80 <- AUL.elections$'1980'
aul80 <- data.frame(Total=aul80$Total,
                   Labor=aul80$vsh_lab,
                   Liberal=aul80$vsh_lib_pty,
                   NationalCountry=aul80$vsh_nat_cntry)

aul80.split <- findpatterns(aul80)

fit80 <- list()
fit80[[1]] <- mvnmix(dat=aul80.split[[2]],components=2,nrep=10,scatter=T)
fit80[[2]] <- mvnmix(dat=aul80.split[[3]],components=2,nrep=10,scatter=T)
fit80[[3]] <- mvnmix(dat=aul80.split[[4]],components=1,nrep=10,scatter=T)

show.marginals(fit80,numdraws=50000)

sr80 <- swingratio(fit80,sims=10000)

##########
## 1983 ##
##########

aul83 <- AUL.elections$'1983'
aul83 <- data.frame(Total=aul83$Total,
                   Labor=aul83$vsh_lab,
                   Liberal=aul83$vsh_lib_pty,
                   NationalParty=aul83$vsh_nat_pty)

aul83.split <- findpatterns(aul83)

fit83 <- list()
fit83[[1]] <- mvnmix(dat=aul83.split[[2]],components=2,nrep=10,scatter=T)
fit83[[2]] <- mvnmix(dat=aul83.split[[3]],components=2,nrep=10,scatter=T)
fit83[[3]] <- mvnmix(dat=aul83.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit83,numdraws=50000)

sr83 <- swingratio(fit83,sims=10000)

##########
## 1984 ##
##########

aul84 <- AUL.elections$'1984'
aul84 <- data.frame(Total=aul84$Total,
                   Labor=aul84$vsh_lab,
                   Liberal=aul84$vsh_lib_pty,
                   NationalParty=aul84$vsh_nat_pty)

aul84.split <- findpatterns(aul84)

fit84 <- list()
fit84[[1]] <- mvnmix(dat=aul84.split[[2]],components=2,nrep=10,scatter=T)
fit84[[2]] <- mvnmix(dat=aul84.split[[3]],components=2,nrep=10,scatter=T)

show.marginals(fit84,numdraws=50000)

sr84 <- swingratio(fit84,sims=10000)

##########
## 1987 ##
##########

aul87 <- AUL.elections$'1987'
aul87 <- data.frame(Total=aul87$Total,
                   Labor=aul87$vsh_lab,
                   Liberal=aul87$vsh_lib_pty,
                   NationalParty=aul87$vsh_nat_pty)

aul87.split <- findpatterns(aul87)

fit87 <- list()
fit87[[1]] <- mvnmix(dat=aul87.split[[3]],components=2,nrep=10,scatter=T)
fit87[[2]] <- mvnmix(dat=aul87.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit87,numdraws=50000)

sr87 <- swingratio(fit87,sims=10000)

##########
## 1990 ##
##########

aul90 <- AUL.elections$'1990'
aul90 <- data.frame(Total=aul90$Total,
                   Labor=aul90$vsh_lab,
                   Liberal=aul90$vsh_lib_pty,
                   NationalParty=aul90$vsh_nat_pty)

aul90.split <- findpatterns(aul90)

fit90 <- list()
fit90[[1]] <- mvnmix(dat=aul90.split[[3]],components=3,nrep=10,scatter=T)
fit90[[2]] <- mvnmix(dat=aul90.split[[4]],components=1,nrep=10,scatter=T)

show.marginals(fit90,numdraws=50000)

sr90 <- swingratio(fit90,sims=10000)

##########
## 1993 ##
##########

aul93 <- AUL.elections$'1993'
aul93 <- data.frame(Total=aul93$Total,
                   Labor=aul93$vsh_lab,
                   Liberal=aul93$vsh_lib_pty,
                   NationalParty=aul93$vsh_nat_pty)

aul93.split <- findpatterns(aul93)

fit93 <- list()
fit93[[1]] <- mvnmix(dat=aul93.split[[5]],components=4,nrep=10,scatter=T)
fit93[[2]] <- mvnmix(dat=aul93.split[[6]],components=1,nrep=10,scatter=T)

show.marginals(fit93,numdraws=50000)

sr93 <- swingratio(fit93,sims=10000)

##########
## 1996 ##
##########

aul96 <- AUL.elections$'1996'
aul96 <- data.frame(Total=aul96$Total,
                   Labor=aul96$vsh_lab,
                   Liberal=aul96$vsh_lib_pty,
                   NationalParty=aul96$vsh_nat_pty)

aul96.split <- findpatterns(aul96)

fit96 <- list()
fit96[[1]] <- mvnmix(dat=aul96.split[[6]],components=2,nrep=10,scatter=T)
fit96[[2]] <- mvnmix(dat=aul96.split[[7]],components=3,nrep=10,scatter=T)

show.marginals(fit96,numdraws=50000)

sr96 <- swingratio(fit96,sims=10000)

##########
## 1998 ##
##########

aul98 <- AUL.elections$'1998'
aul98 <- data.frame(Total=aul98$Total,
                   Labor=aul98$vsh_lab,
                   Liberal=aul98$vsh_lib_pty,
                   NationalParty=aul98$vsh_nat_pty)

aul98.split <- findpatterns(aul98)

fit98 <- list()
fit98[[1]] <- mvnmix(dat=aul98.split[[3]],components=1,nrep=10,scatter=T)
fit98[[2]] <- mvnmix(dat=aul98.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit98,numdraws=50000)

sr98 <- swingratio(fit98,sims=10000)

##########
## 2001 ##
##########

aul01 <- AUL.elections$'2001'
aul01 <- data.frame(Total=aul01$Total,
                   Labor=aul01$vsh_lab,
                   Liberal=aul01$vsh_lib_pty,
                   NationalParty=aul01$vsh_nat_pty)

aul01.split <- findpatterns(aul01)

fit01 <- list()
fit01[[1]] <- mvnmix(dat=aul01.split[[5]],components=2,nrep=10,scatter=T)
fit01[[2]] <- mvnmix(dat=aul01.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit01,numdraws=50000)

sr01 <- swingratio(fit01,sims=10000)

##########
## 2004 ##
##########

aul04 <- AUL.elections$'2004'
aul04 <- data.frame(Total=aul04$Total,
                   Labor=aul04$vsh_lab,
                   Liberal=aul04$vsh_lib_pty,
                   NationalParty=aul04$vsh_nat_pty)

aul04.split <- findpatterns(aul04)

fit04 <- list()
fit04[[1]] <- mvnmix(dat=aul04.split[[4]],components=4,nrep=10,scatter=T)
fit04[[2]] <- mvnmix(dat=aul04.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit04,numdraws=50000)

sr04 <- swingratio(fit04,sims=10000)


# Assemble results (see Linzer's seatsvotes code).
AULresults <- list(sr46,sr49,sr51,sr54,sr55,sr58,sr61,sr63,sr66,sr69,sr72,sr74,sr75,sr77,sr80,sr83,sr84,sr87,sr90,sr93,sr96,sr98,sr01,sr04)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(AULresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(AULresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(AULresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(AULresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(AULresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]


#####################
###### Canada #######
#####################

# see Drew's code in "Linzer-seatsvotes.R"

# 1945-1980: new code
# NOTE: We are omitting parties listed under "Other" from the estimation and only include parties that won seats in the election.
# Source: http://www.sfu.ca/~aheard/elections/1867-present.html

# set random number seed for replicability
set.seed(1234)

##########
## 1945 ##
##########

ca45 <- CAN.elections$'1945'
ca45 <- data.frame(Total=ca45$Total,
                   LIB=ca45$vsh_lib,
                   PC=ca45$vsh_prog_cons,
                   CCF=ca45$vsh_coop_com_fed,
                   SC=ca45$vsh_soc_cred)

ca45.split <- findpatterns(ca45)

fit45 <- list()
fit45[[1]] <- mvnmix(dat=ca45.split[[10]],components=1,nrep=10,scatter=T)
fit45[[2]] <- mvnmix(dat=ca45.split[[11]],components=2,nrep=10,scatter=T)
fit45[[3]] <- mvnmix(dat=ca45.split[[12]],components=1,nrep=10,scatter=T)
fit45[[4]] <- mvnmix(dat=ca45.split[[13]],components=2,nrep=10,scatter=T)
fit45[[5]] <- mvnmix(dat=ca45.split[[14]],components=1,nrep=10,scatter=T)
fit45[[6]] <- mvnmix(dat=ca45.split[[15]],components=3,nrep=10,scatter=T)
fit45[[7]] <- mvnmix(dat=ca45.split[[16]],components=2,nrep=10,scatter=T)

show.marginals(fit45,numdraws=50000)

sr45 <- swingratio(fit45,sims=10000)

##########
## 1949 ##
##########

ca49 <- CAN.elections$'1949'
ca49 <- data.frame(Total=ca49$Total,
                   LIB=ca49$vsh_lib,
                   PC=ca49$vsh_prog_cons,
                   CCF=ca49$vsh_coop_com_fed,
                   SC=ca49$vsh_soc_cred)

ca49.split <- findpatterns(ca49)

fit49 <- list()
fit49[[1]] <- mvnmix(dat=ca49.split[[7]],components=2,nrep=10,scatter=T)
fit49[[2]] <- mvnmix(dat=ca49.split[[9]],components=3,nrep=10,scatter=T)
fit49[[3]] <- mvnmix(dat=ca49.split[[10]],components=3,nrep=10,scatter=T)

show.marginals(fit49,numdraws=50000)

sr49 <- swingratio(fit49,sims=10000)

##########
## 1953 ##
##########

ca53 <- CAN.elections$'1953'
ca53 <- data.frame(Total=ca53$Total,
                   LIB=ca53$vsh_lib,
                   PC=ca53$vsh_prog_cons,
                   CCF=ca53$vsh_coop_com_fed,
                   SC=ca53$vsh_soc_cred)

ca53.split <- findpatterns(ca53)

fit53 <- list()
fit53[[1]] <- mvnmix(dat=ca53.split[[6]],components=2,nrep=10,scatter=T)
fit53[[2]] <- mvnmix(dat=ca53.split[[7]],components=2,nrep=10,scatter=T)
fit53[[3]] <- mvnmix(dat=ca53.split[[8]],components=2,nrep=10,scatter=T)
fit53[[4]] <- mvnmix(dat=ca53.split[[9]],components=2,nrep=10,scatter=T)
fit53[[5]] <- mvnmix(dat=ca53.split[[10]],components=1,nrep=10,scatter=T)

show.marginals(fit53,numdraws=50000)

sr53 <- swingratio(fit53,sims=10000)

##########
## 1957 ##
##########

ca57 <- CAN.elections$'1957'
ca57 <- data.frame(Total=ca57$Total,
                   LIB=ca57$vsh_lib,
                   PC=ca57$vsh_prog_cons,
                   CCF=ca57$vsh_coop_com_fed,
                   SC=ca57$vsh_soc_cred)

ca57.split <- findpatterns(ca57)

fit57 <- list()
fit57[[1]] <- mvnmix(dat=ca57.split[[4]],components=2,nrep=10,scatter=T)
fit57[[2]] <- mvnmix(dat=ca57.split[[5]],components=2,nrep=10,scatter=T)
fit57[[3]] <- mvnmix(dat=ca57.split[[6]],components=2,nrep=10,scatter=T)
fit57[[4]] <- mvnmix(dat=ca57.split[[7]],components=2,nrep=10,scatter=T)

show.marginals(fit57,numdraws=50000)

sr57 <- swingratio(fit57,sims=10000)

##########
## 1958 ##
##########

ca58 <- CAN.elections$'1958'
ca58 <- data.frame(Total=ca58$Total,
                   LIB=ca58$vsh_lib,
                   PC=ca58$vsh_prog_cons,
                   CCF=ca58$vsh_coop_com_fed,
                   SC=ca58$vsh_soc_cred)

ca58.split <- findpatterns(ca58)

fit58 <- list()
fit58[[1]] <- mvnmix(dat=ca58.split[[2]],components=1,nrep=10,scatter=T)
fit58[[2]] <- mvnmix(dat=ca58.split[[3]],components=2,nrep=10,scatter=T)
fit58[[3]] <- mvnmix(dat=ca58.split[[4]],components=3,nrep=10,scatter=T)
fit58[[4]] <- mvnmix(dat=ca58.split[[5]],components=1,nrep=10,scatter=T)

show.marginals(fit58,numdraws=50000)

sr58 <- swingratio(fit58,sims=10000)

##########
## 1962 ##
##########

ca62 <- CAN.elections$'1962'
ca62 <- data.frame(Total=ca62$Total,
                   LIB=ca62$vsh_lib,
                   PC=ca62$vsh_prog_cons,
                   ND=ca62$vsh_new_dem,
                   SC=ca62$vsh_soc_cred)

ca62.split <- findpatterns(ca62)

fit62 <- list()
fit62[[1]] <- mvnmix(dat=ca62.split[[3]],components=2,nrep=10,scatter=T)
fit62[[2]] <- mvnmix(dat=ca62.split[[4]],components=2,nrep=10,scatter=T)
fit62[[3]] <- mvnmix(dat=ca62.split[[5]],components=3,nrep=10,scatter=T)
fit62[[4]] <- mvnmix(dat=ca62.split[[6]],components=3,nrep=10,scatter=T)

show.marginals(fit62,numdraws=50000)

sr62 <- swingratio(fit62,sims=10000)

##########
## 1963 ##
##########

ca63 <- CAN.elections$'1963'
ca63 <- data.frame(Total=ca63$Total,
                   LIB=ca63$vsh_lib,
                   PC=ca63$vsh_prog_cons,
                   ND=ca63$vsh_new_dem,
                   SC=ca63$vsh_soc_cred)

ca63.split <- findpatterns(ca63)

fit63 <- list()
fit63[[1]] <- mvnmix(dat=ca63.split[[2]],components=2,nrep=10,scatter=T)
fit63[[2]] <- mvnmix(dat=ca63.split[[3]],components=2,nrep=10,scatter=T)
fit63[[3]] <- mvnmix(dat=ca63.split[[4]],components=2,nrep=10,scatter=T)
fit63[[4]] <- mvnmix(dat=ca63.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit63,numdraws=50000)

sr63 <- swingratio(fit63,sims=10000)

##########
## 1965 ##
##########

ca65 <- CAN.elections$'1965'
ca65 <- data.frame(Total=ca65$Total,
                   LIB=ca65$vsh_lib,
                   PC=ca65$vsh_prog_cons,
                   ND=ca65$vsh_new_dem,
                   SC=ca65$vsh_soc_cred)

ca65.split <- findpatterns(ca65)

fit65 <- list()
fit65[[1]] <- mvnmix(dat=ca65.split[[1]],components=2,nrep=10,scatter=T)
fit65[[2]] <- mvnmix(dat=ca65.split[[3]],components=2,nrep=10,scatter=T)
fit65[[3]] <- mvnmix(dat=ca65.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit65,numdraws=50000)

sr65 <- swingratio(fit65,sims=10000)

##########
## 1968 ##
##########

ca68 <- CAN.elections$'1968'
ca68 <- data.frame(Total=ca68$Total,
                   LIB=ca68$vsh_lib,
                   PC=ca68$vsh_prog_cons,
                   ND=ca68$vsh_new_dem,
                   SC=ca68$vsh_soc_cred)

ca68.split <- findpatterns(ca68)

fit68 <- list()
fit68[[1]] <- mvnmix(dat=ca68.split[[5]],components=1,nrep=10,scatter=T)
fit68[[2]] <- mvnmix(dat=ca68.split[[6]],components=1,nrep=10,scatter=T)

show.marginals(fit68,numdraws=50000)

sr68 <- swingratio(fit68,sims=10000)

##########
## 1972 ##
##########

ca72 <- CAN.elections$'1972'
ca72 <- data.frame(Total=ca72$Total,
                   LIB=ca72$vsh_lib,
                   PC=ca72$vsh_prog_cons,
                   ND=ca72$vsh_new_dem,
                   SC=ca72$vsh_soc_cred)

ca72.split <- findpatterns(ca72)

fit72 <- list()
fit72[[1]] <- mvnmix(dat=ca72.split[[2]],components=3,nrep=10,scatter=T)
fit72[[2]] <- mvnmix(dat=ca72.split[[3]],components=2,nrep=10,scatter=T)
fit72[[3]] <- mvnmix(dat=ca72.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit72,numdraws=50000)

sr72 <- swingratio(fit72,sims=10000)

##########
## 1974 ##
##########

ca74 <- CAN.elections$'1974'
ca74 <- data.frame(Total=ca74$Total,
                   LIB=ca74$vsh_lib,
                   PC=ca74$vsh_prog_cons,
                   ND=ca74$vsh_new_dem,
                   SC=ca74$vsh_soc_cred)

ca74.split <- findpatterns(ca74)

fit74 <- list()
fit74[[1]] <- mvnmix(dat=ca74.split[[3]],components=2,nrep=10,scatter=T)
fit74[[2]] <- mvnmix(dat=ca74.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit74,numdraws=50000)

sr74 <- swingratio(fit74,sims=10000)

##########
## 1979 ##
##########

ca79 <- CAN.elections$'1979'
ca79 <- data.frame(Total=ca79$Total,
                   LIB=ca79$vsh_lib,
                   PC=ca79$vsh_prog_cons,
                   ND=ca79$vsh_new_dem,
                   SC=ca79$vsh_soc_cred)

ca79.split <- findpatterns(ca79)

fit79 <- list()
fit79[[1]] <- mvnmix(dat=ca79.split[[1]],components=2,nrep=10,scatter=T)
fit79[[2]] <- mvnmix(dat=ca79.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit79,numdraws=50000)

sr79 <- swingratio(fit79,sims=10000)

##########
## 1980 ##
##########

ca80 <- CAN.elections$'1980'
ca80 <- data.frame(Total=ca80$Total,
                   LIB=ca80$vsh_lib,
                   PC=ca80$vsh_prog_cons,
                   ND=ca80$vsh_new_dem,
                   SC=ca80$vsh_soc_cred)

ca80.split <- findpatterns(ca80)

fit80 <- list()
fit80[[1]] <- mvnmix(dat=ca80.split[[2]],components=2,nrep=10,scatter=T)
fit80[[2]] <- mvnmix(dat=ca80.split[[3]],components=3,nrep=10,scatter=T)

show.marginals(fit80,numdraws=50000)

sr80 <- swingratio(fit80,sims=10000)

##########
## 1984 ##
##########

ca84 <- CAN.elections$'1984'
ca84 <- data.frame(Total=ca84$Total,
                   LIB=ca84$vsh_lib,
                   PC=ca84$vsh_prog_cons,
                   NDP=ca84$vsh_new_dem)

ca84.split <- findpatterns(ca84)

fit84 <- list()
fit84[[1]] <- mvnmix(dat=ca84.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit84,numdraws=50000)

sr84 <- swingratio(fit84,sims=10000)

##########
## 1988 ##
##########

ca88 <- CAN.elections$'1988'
ca88 <- data.frame(Total=ca88$Total,
                   LIB=ca88$vsh_lib,
                   PC=ca88$vsh_prog_cons,
                   NDP=ca88$vsh_new_dem)

ca88.split <- findpatterns(ca88)

fit88 <- list()
fit88[[1]] <- mvnmix(dat=ca88.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit88,numdraws=50000)

sr88 <- swingratio(fit88,sims=10000)

##########
## 1993 ##
##########

ca93 <- CAN.elections$'1993'
ca93 <- data.frame(Total=ca93$Total,
                   LIB=ca93$vsh_lib,
                   PC=ca93$vsh_prog_cons,
                   NDP=ca93$vsh_new_dem,
                   REF=ca93$vsh_ref,
                   BQ=ca93$vsh_bl_queb)

ca93.split <- findpatterns(ca93)

fit93 <- list()
fit93[[1]] <- mvnmix(dat=ca93.split[[2]],components=2,nrep=10,scatter=T)
fit93[[2]] <- mvnmix(dat=ca93.split[[3]],components=2,nrep=10,scatter=T)
fit93[[3]] <- mvnmix(dat=ca93.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit93,numdraws=50000)

sr93 <- swingratio(fit93,sims=10000)

##########
## 1997 ##
##########

ca97 <- CAN.elections$'1997'
ca97 <- data.frame(Total=ca97$Total,
                   LIB=ca97$vsh_lib,
                   PC=ca97$vsh_prog_cons,
                   NDP=ca97$vsh_new_dem,
                   REF=ca97$vsh_ref,
                   BQ=ca97$vsh_bl_queb)

ca97.split <- findpatterns(ca97)

fit97 <- list()
fit97[[1]] <- mvnmix(dat=ca97.split[[1]],components=2,nrep=10,scatter=T)
fit97[[2]] <- mvnmix(dat=ca97.split[[2]],components=2,nrep=10,scatter=T)
fit97[[3]] <- mvnmix(dat=ca97.split[[3]],components=2,nrep=10,scatter=T)
fit97[[4]] <- mvnmix(dat=ca97.split[[4]],components=1,nrep=10,scatter=T)

show.marginals(fit97,numdraws=50000)

sr97 <- swingratio(fit97,sims=10000)

##########
## 2000 ##
##########

ca00 <- CAN.elections$'2000'
ca00 <- data.frame(Total=ca00$Total,
                   LIB=ca00$vsh_lib,
                   PC=ca00$vsh_prog_cons,
                   NDP=ca00$vsh_new_dem,
                   CA=ca00$vsh_can_all,
                   BQ=ca00$vsh_bl_queb)

ca00.split <- findpatterns(ca00)

fit00 <- list()
fit00[[1]] <- mvnmix(dat=ca00.split[[1]],components=1,nrep=10,scatter=T)
fit00[[2]] <- mvnmix(dat=ca00.split[[6]],components=3,nrep=10,scatter=T)
fit00[[3]] <- mvnmix(dat=ca00.split[[7]],components=3,nrep=10,scatter=T)

show.marginals(fit00,numdraws=50000)

sr00 <- swingratio(fit00,sims=10000)

##########
## 2004 ##
##########

ca04 <- CAN.elections$'2004'
ca04 <- data.frame(Total=ca04$Total,
                   LIB=ca04$vsh_lib,
                   C=ca04$vsh_cons,
                   NDP=ca04$vsh_new_dem,
                   BQ=ca04$vsh_bl_queb)

ca04.split <- findpatterns(ca04)

fit04 <- list()
fit04[[1]] <- mvnmix(dat=ca04.split[[1]],components=2,nrep=10,scatter=T)
fit04[[2]] <- mvnmix(dat=ca04.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit04,numdraws=50000)

sr04 <- swingratio(fit04,sims=10000)

##########
## 2006 ##
##########

ca06 <- CAN.elections$'2006'
ca06 <- data.frame(Total=ca06$Total,
                   LIB=ca06$vsh_lib,
                   C=ca06$vsh_cons,
                   NDP=ca06$vsh_new_dem,
                   BQ=ca06$vsh_bl_queb)

ca06.split <- findpatterns(ca06)

fit06 <- list()
fit06[[1]] <- mvnmix(dat=ca06.split[[1]],components=2,nrep=10,scatter=T)
fit06[[2]] <- mvnmix(dat=ca06.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit06,numdraws=50000)

sr06 <- swingratio(fit06,sims=10000)

##########
## 2008 ##
##########

ca08 <- CAN.elections$'2008'
ca08 <- data.frame(Total=ca08$Total,
                   LIB=ca08$vsh_lib,
                   C=ca08$vsh_cons,
                   NDP=ca08$vsh_new_dem,
                   BQ=ca08$vsh_bl_queb)

ca08.split <- findpatterns(ca08)

fit08 <- list()
fit08[[1]] <- mvnmix(dat=ca08.split[[3]],components=2,nrep=10,scatter=T)
fit08[[2]] <- mvnmix(dat=ca08.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit08,numdraws=50000)

sr08 <- swingratio(fit08,sims=10000)


# Assemble results (see Linzer's seatsvotes code).
CANresults <- list(sr45,sr49,sr53,sr57,sr58,sr62,sr63,sr65,sr68,sr72,sr74,sr79,sr80,sr84,sr88,sr93,sr97,sr00,sr04,sr06,sr08)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(CANresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(CANresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(CANresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(CANresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(CANresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]


##################
##### France #####
##################

# NOTE: The 1986 election was held using party-list PR, so it is dropped from this analysis.
# Source: http://en.wikipedia.org/wiki/French_legislative_election,_1978 (and other years)

# set random number seed to guarantee perfect replicability
set.seed(1234)

##########
## 1973 ##
##########

fra73 <- FRA.elections$'1973'

fra73 <- data.frame(Total=fra73$Total,
                   UnionLeft=fra73$Union.of.the.Left..PCF..PS.MGRS..PSU..OtherLeft..,
                   Reform=fra73$Reform.Movement..MR...OtherReform.,
                   Right=fra73$Right.Parties..URP..UDR..IR..CDP....Other.Right.)

fra73.split <- findpatterns(fra73)

fit73 <- list()
fit73[[1]] <- mvnmix(dat=fra73.split[[3]],components=3,nrep=10,scatter=T)
fit73[[2]] <- mvnmix(dat=fra73.split[[4]],components=3,nrep=10,scatter=T)
fit73[[3]] <- mvnmix(dat=fra73.split[[5]],components=4,nrep=10,scatter=T)

show.marginals(fit73,numdraws=50000)

sr73 <- swingratio(fit73,sims=10000)

##########
## 1978 ##
##########

fra78 <- FRA.elections$'1978'
fra78 <- data.frame(Total=fra78$Total,
                   UnionLeft=fra78$Union.of.the.Left..PCF..PS..MRG.,
                   Right=fra78$Right.Grouping..RPR..UDF...Other.)
                   
fra78.split <- findpatterns(fra78)

fit78 <- list()
fit78[[1]] <- mvnmix(dat=fra78.split[[3]],components=1,nrep=10,scatter=T)

show.marginals(fit78,numdraws=50000)

sr78 <- swingratio(fit78,sims=10000)

##########
## 1981 ##
##########

fra81 <- FRA.elections$'1981'
fra81 <- data.frame(Total=fra81$Total,
                   UnionLeft=fra81$Union.of.the.Left..PS..PCF...Other.Left.,
                   UnionNewMajority=fra81$Union.for.a.New.Majority..RPR..UDF..Other.Right.)
                   
fra81 <- fra81[1:469,]
                   
fra81.split <- findpatterns(fra81)

fit81 <- list()
fit81[[1]] <- mvnmix(dat=fra81.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit81,numdraws=50000)

sr81 <- swingratio(fit81,sims=10000)

##########
## 1988 ##
##########

fra88 <- FRA.elections$'1988'
fra88 <- data.frame(Total=fra88$Total,
                   Left=fra88$Left.Parties..PS..Left.Radicals...Other.Left.,
                   Right=fra88$Right.Parties..RPR..UDF...Other.Right.,
                   Communists=fra88$Communists,
                   NationalFront=fra88$National.Front)

fra88.split <- findpatterns(fra88)

fit88 <- list()
fit88[[1]] <- mvnmix(dat=fra88.split[[1]],components=3,nrep=10,scatter=T)
fit88[[2]] <- mvnmix(dat=fra88.split[[4]],components=3,nrep=10,scatter=T)
fit88[[3]] <- mvnmix(dat=fra88.split[[5]],components=3,nrep=10,scatter=T)
fit88[[4]] <- mvnmix(dat=fra88.split[[7]],components=3,nrep=10,scatter=T)
fit88[[5]] <- mvnmix(dat=fra88.split[[8]],components=3,nrep=10,scatter=T)

show.marginals(fit88,numdraws=50000)

sr88 <- swingratio(fit88,sims=10000)

##########
## 1993 ##
##########

fra93 <- FRA.elections$'1993'
fra93 <- data.frame(Total=fra93$Total,
                   Left=fra93$PS..PCF...Mvt_Rad_Gau...Other.Left.,
                   Right=fra93$Right.Grouping..RPR..UDF..Other.Right.,
                   NationalFront=fra93$National.Front,
                   Greens=fra93$Green.Parties..LesVerts..GE.)
                   
fra93.split <- findpatterns(fra93)

fit93 <- list()
fit93[[1]] <- mvnmix(dat=fra93.split[[2]],components=1,nrep=10,scatter=T)
fit93[[2]] <- mvnmix(dat=fra93.split[[3]],components=1,nrep=10,scatter=T)
fit93[[3]] <- mvnmix(dat=fra93.split[[5]],components=1,nrep=10,scatter=T)
fit93[[4]] <- mvnmix(dat=fra93.split[[6]],components=3,nrep=10,scatter=T)
fit93[[5]] <- mvnmix(dat=fra93.split[[7]],components=3,nrep=10,scatter=T)

show.marginals(fit93,numdraws=50000)

sr93 <- swingratio(fit93,sims=10000)

##########
## 1997 ##
##########

fra97 <- FRA.elections$'1997'
fra97 <- data.frame(Total=fra97$Total,
                   PluralLeft=fra97$Plural.Left..PS..PRS..PCF..Greens...CitizensMovement.,
                   Right=fra97$Right.Coalition..RPR..UDF..LDI.and.Other.Right.,
                   NationalFront=fra97$National.Front)
                   
fra97.split <- findpatterns(fra97)

fit97 <- list()
fit97[[1]] <- mvnmix(dat=fra97.split[[2]],components=4,nrep=10,scatter=T)
fit97[[2]] <- mvnmix(dat=fra97.split[[4]],components=2,nrep=10,scatter=T)
fit97[[3]] <- mvnmix(dat=fra97.split[[5]],components=2,nrep=10,scatter=T)
fit97[[4]] <- mvnmix(dat=fra97.split[[6]],components=1,nrep=10,scatter=T)

show.marginals(fit97,numdraws=50000)

sr97 <- swingratio(fit97,sims=10000)

##########
## 2002 ##
##########

fra02 <- FRA.elections$'2002'
fra02 <- data.frame(Total=fra02$Total,
                   UnitedLeft=fra02$United.Left..PS..PCF..PRG..Greens.,
                   Right=fra02$Right.Parties..UMP..UDF..RPF..MPF..Lib_Dem.and.Other.Right.,
                   NationalFront=fra02$NationalFront)

fra02.split <- findpatterns(fra02)

fit02 <- list()
fit02[[1]] <- mvnmix(dat=fra02.split[[2]],components=1,nrep=10,scatter=T)
fit02[[2]] <- mvnmix(dat=fra02.split[[4]],components=2,nrep=10,scatter=T)
fit02[[3]] <- mvnmix(dat=fra02.split[[5]],components=2,nrep=10,scatter=T)
fit02[[4]] <- mvnmix(dat=fra02.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit02,numdraws=50000)

sr02 <- swingratio(fit02,sims=10000)

# Assemble results (see Linzer's seatsvotes code).
FRAresults <- list(sr73,sr78,sr81,sr88,sr93,sr97,sr02)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(FRAresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(FRAresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(FRAresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(FRAresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(FRAresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]


#######################
##### New Zealand #####
#######################

# Source: http://en.wikipedia.org/wiki/Elections_in_New_Zealand

# set random number seed for replicability
set.seed(1234)

##########
## 1946 ##
##########

nz46 <- NZ.elections$'1946'
nz46 <- data.frame(Total=nz46$Total,
                   Labour=nz46$vsh_lab,
                   National=nz46$vsh_nat)

nz46.split <- findpatterns(nz46)

fit46 <- list()
fit46[[1]] <- mvnmix(dat=nz46.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit46,numdraws=50000)

sr46 <- swingratio(fit46,sims=10000)

##########
## 1949 ##
##########

nz49 <- NZ.elections$'1949'
nz49 <- data.frame(Total=nz49$Total,
                   Labour=nz49$vsh_lab,
                   National=nz49$vsh_nat)

nz49.split <- findpatterns(nz49)

fit49 <- list()
fit49[[1]] <- mvnmix(dat=nz49.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit49,numdraws=50000)

sr49 <- swingratio(fit49,sims=10000)

##########
## 1951 ##
##########

nz51 <- NZ.elections$'1951'
nz51 <- data.frame(Total=nz51$Total,
                   Labour=nz51$vsh_lab,
                   National=nz51$vsh_nat)

nz51.split <- findpatterns(nz51)

fit51 <- list()
fit51[[1]] <- mvnmix(dat=nz51.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit51,numdraws=50000)

sr51 <- swingratio(fit51,sims=10000)

##########
## 1954 ##
##########

nz54 <- NZ.elections$'1954'
nz54 <- data.frame(Total=nz54$Total,
                   Labour=nz54$vsh_lab,
                   National=nz54$vsh_nat)

nz54.split <- findpatterns(nz54)

fit54 <- list()
fit54[[1]] <- mvnmix(dat=nz54.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit54,numdraws=50000)

sr54 <- swingratio(fit54,sims=10000)

##########
## 1957 ##
##########

nz57 <- NZ.elections$'1957'
nz57 <- data.frame(Total=nz57$Total,
                   Labour=nz57$vsh_lab,
                   National=nz57$vsh_nat)

nz57.split <- findpatterns(nz57)

fit57 <- list()
fit57[[1]] <- mvnmix(dat=nz57.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit57,numdraws=50000)

sr57 <- swingratio(fit57,sims=10000)

##########
## 1960 ##
##########

nz60 <- NZ.elections$'1960'
nz60 <- data.frame(Total=nz60$Total,
                   Labour=nz60$vsh_lab,
                   National=nz60$vsh_nat)

nz60.split <- findpatterns(nz60)

fit60 <- list()
fit60[[1]] <- mvnmix(dat=nz60.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit60,numdraws=50000)

sr60 <- swingratio(fit60,sims=10000)

##########
## 1963 ##
##########

nz63 <- NZ.elections$'1963'
nz63 <- data.frame(Total=nz63$Total,
                   Labour=nz63$vsh_lab,
                   National=nz63$vsh_nat)

nz63.split <- findpatterns(nz63)

fit63 <- list()
fit63[[1]] <- mvnmix(dat=nz63.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit63,numdraws=50000)

sr63 <- swingratio(fit63,sims=10000)

##########
## 1966 ##
##########

nz66 <- NZ.elections$'1966'
nz66 <- data.frame(Total=nz66$Total,
                   Labour=nz66$vsh_lab,
                   National=nz66$vsh_nat)

nz66.split <- findpatterns(nz66)

fit66 <- list()
fit66[[1]] <- mvnmix(dat=nz66.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit66,numdraws=50000)

sr66 <- swingratio(fit66,sims=10000)

##########
## 1969 ##
##########

nz69 <- NZ.elections$'1969'
nz69 <- data.frame(Total=nz69$Total,
                   Labour=nz69$vsh_lab,
                   National=nz69$vsh_nat)

nz69.split <- findpatterns(nz69)

fit69 <- list()
fit69[[1]] <- mvnmix(dat=nz69.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit69,numdraws=50000)

sr69 <- swingratio(fit69,sims=10000)

##########
## 1972 ##
##########

nz72 <- NZ.elections$'1972'
nz72 <- data.frame(Total=nz72$Total,
                   Labour=nz72$vsh_lab,
                   National=nz72$vsh_nat)

nz72.split <- findpatterns(nz72)

fit72 <- list()
fit72[[1]] <- mvnmix(dat=nz72.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit72,numdraws=50000)

sr72 <- swingratio(fit72,sims=10000)

##########
## 1975 ##
##########

nz75 <- NZ.elections$'1975'
nz75 <- data.frame(Total=nz75$Total,
                   Labour=nz75$vsh_lab,
                   National=nz75$vsh_nat)

nz75.split <- findpatterns(nz75)

fit75 <- list()
fit75[[1]] <- mvnmix(dat=nz75.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit75,numdraws=50000)

sr75 <- swingratio(fit75,sims=10000)

##########
## 1978 ##
##########

nz78 <- NZ.elections$'1978'
nz78 <- data.frame(Total=nz78$Total,
                   Labour=nz78$vsh_lab,
                   National=nz78$vsh_nat)

nz78.split <- findpatterns(nz78)

fit78 <- list()
fit78[[1]] <- mvnmix(dat=nz78.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit78,numdraws=50000)

sr78 <- swingratio(fit78,sims=10000)

##########
## 1981 ##
##########

nz81 <- NZ.elections$'1981'
nz81 <- data.frame(Total=nz81$Total,
                   Labour=nz81$vsh_lab,
                   National=nz81$vsh_nat)

nz81.split <- findpatterns(nz81)

fit81 <- list()
fit81[[1]] <- mvnmix(dat=nz81.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit81,numdraws=50000)

sr81 <- swingratio(fit81,sims=10000)

##########
## 1984 ##
##########

nz84 <- NZ.elections$'1984'
nz84 <- data.frame(Total=nz84$Total,
                   Labour=nz84$vsh_lab,
                   National=nz84$vsh_nat)

nz84.split <- findpatterns(nz84)

fit84 <- list()
fit84[[1]] <- mvnmix(dat=nz84.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit84,numdraws=50000)

sr84 <- swingratio(fit84,sims=10000)

##########
## 1987 ##
##########

nz87 <- NZ.elections$'1987'
nz87 <- data.frame(Total=nz87$Total,
                   Labour=nz87$vsh_lab,
                   National=nz87$vsh_nat)

nz87.split <- findpatterns(nz87)

fit87 <- list()
fit87[[1]] <- mvnmix(dat=nz87.split[[1]],components=2,nrep=10,scatter=T)

show.marginals(fit87,numdraws=50000)

sr87 <- swingratio(fit87,sims=10000)

##########
## 1990 ##
##########

nz90 <- NZ.elections$'1990'
nz90 <- data.frame(Total=nz90$Total,
                   Labour=nz90$vsh_lab,
                   National=nz90$vsh_nat)

nz90.split <- findpatterns(nz90)

fit90 <- list()
fit90[[1]] <- mvnmix(dat=nz90.split[[1]],components=3,nrep=10,scatter=T)

show.marginals(fit90,numdraws=50000)

sr90 <- swingratio(fit90,sims=10000)

##########
## 1996 ##
##########

nz96 <- NZ.elections$'1996'
nz96 <- data.frame(Total=nz96$Total,
                   Labour=nz96$vsh_lab,
                   National=nz96$vsh_nat,
                   Others=nz96$vsh_others)

nz96.split <- findpatterns(nz96)

fit96 <- list()
fit96[[1]] <- mvnmix(dat=nz96.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit96,numdraws=50000)

sr96 <- swingratio(fit96,sims=10000)

##########
## 1999 ##
##########

nz99 <- NZ.elections$'1999'
nz99 <- data.frame(Total=nz99$Total,
                   Labour=nz99$vsh_lab,
                   National=nz99$vsh_nat,
                   Others=nz99$vsh_others)

nz99.split <- findpatterns(nz99)

fit99 <- list()
fit99[[1]] <- mvnmix(dat=nz99.split[[2]],components=2,nrep=10,scatter=T)

show.marginals(fit99,numdraws=50000)

sr99 <- swingratio(fit99,sims=10000)


# Assemble results (see Linzer's seatsvotes code).
NZresults <- list(sr46,sr49,sr51,sr54,sr57,sr60,sr63,sr66,sr69,sr72,sr75,sr78,sr81,sr84,sr87,sr90,sr96,sr99)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(NZresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(NZresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(NZresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(NZresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(NZresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]


##########################
##### United Kingdom #####
##########################

# see also Drew Linzer's code in "Linzer-seatsvotes.R"

# Source: http://en.wikipedia.org/wiki/United_Kingdom_general_election,_1987 (and other years)

# set random number seed to guarantee perfect replicability
set.seed(1234)

##########
## 1945 ##
##########

uk45 <- UK.elections$'1945'
uk45 <- data.frame(Total=uk45$Total,
                   Labour=uk45$vsh_lab,
                   Conservative=uk45$vsh_cons,
                   Liberal=uk45$vsh_lib)

uk45.split <- findpatterns(uk45)

fit45 <- list()
fit45[[1]] <- mvnmix(dat=uk45.split[[5]],components=2,nrep=10,scatter=T)
fit45[[2]] <- mvnmix(dat=uk45.split[[6]],components=2,nrep=10,scatter=T)
fit45[[3]] <- mvnmix(dat=uk45.split[[7]],components=2,nrep=10,scatter=T)

show.marginals(fit45,numdraws=50000)

sr45 <- swingratio(fit45,sims=10000)

##########
## 1950 ##
##########

uk50 <- UK.elections$'1950'
uk50 <- data.frame(Total=uk50$Total,
                   Labour=uk50$vsh_lab,
                   Alliance=uk50$vsh_cons+uk50$vsh_nat_lib,
                   Liberal=uk50$vsh_lib,
                   Regional=uk50$vsh_irish_nat)

uk50.split <- findpatterns(uk50)

fit50 <- list()
fit50[[1]] <- mvnmix(dat=uk50.split[[4]],components=1,nrep=10,scatter=T)
fit50[[2]] <- mvnmix(dat=uk50.split[[5]],components=1,nrep=10,scatter=T)
fit50[[3]] <- mvnmix(dat=uk50.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit50,numdraws=50000)

sr50 <- swingratio(fit50,sims=10000)

##########
## 1951 ##
##########

uk51 <- UK.elections$'1951'
uk51 <- data.frame(Total=uk51$Total,
                   Labour=uk51$vsh_lab,
                   Alliance=uk51$vsh_cons+uk51$vsh_nat_lib,
                   Liberal=uk51$vsh_lib,
                   Regional=uk51$vsh_irish_lab)

uk51.split <- findpatterns(uk51)

fit51 <- list()
fit51[[1]] <- mvnmix(dat=uk51.split[[4]],components=2,nrep=10,scatter=T)
fit51[[2]] <- mvnmix(dat=uk51.split[[5]],components=2,nrep=10,scatter=T)
fit51[[3]] <- mvnmix(dat=uk51.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit51,numdraws=50000)

sr51 <- swingratio(fit51,sims=10000)

##########
## 1955 ##
##########

uk55 <- UK.elections$'1955'
uk55 <- data.frame(Total=uk55$Total,
                   Labour=uk55$vsh_lab,
                   Conservative=uk55$vsh_cons,
                   Liberal=uk55$vsh_lib,
				   Regional=uk55$vsh_sinnfein)

uk55.split <- findpatterns(uk55)

fit55 <- list()
fit55[[1]] <- mvnmix(dat=uk55.split[[4]],components=3,nrep=10,scatter=T)
fit55[[2]] <- mvnmix(dat=uk55.split[[5]],components=2,nrep=10,scatter=T)
fit55[[3]] <- mvnmix(dat=uk55.split[[6]],components=3,nrep=10,scatter=T)

show.marginals(fit55,numdraws=50000)

sr55 <- swingratio(fit55,sims=10000)

##########
## 1959 ##
##########

uk59 <- UK.elections$'1959'
uk59 <- data.frame(Total=uk59$Total,
                   Labour=uk59$vsh_lab,
                   Conservative=uk59$vsh_cons,
                   Liberal=uk59$vsh_lib)

uk59.split <- findpatterns(uk59)

fit59 <- list()
fit59[[1]] <- mvnmix(dat=uk59.split[[4]],components=1,nrep=10,scatter=T)
fit59[[2]] <- mvnmix(dat=uk59.split[[5]],components=1,nrep=10,scatter=T)
fit59[[3]] <- mvnmix(dat=uk59.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit59,numdraws=50000)

sr59 <- swingratio(fit59,sims=10000)

##########
## 1964 ##
##########

uk64 <- UK.elections$'1964'
uk64 <- data.frame(Total=uk64$Total,
                   Labour=uk64$vsh_lab,
                   Conservative=uk64$vsh_cons,
                   Liberal=uk64$vsh_lib)

uk64.split <- findpatterns(uk64)

fit64 <- list()
fit64[[1]] <- mvnmix(dat=uk64.split[[4]],components=2,nrep=10,scatter=T)
fit64[[2]] <- mvnmix(dat=uk64.split[[5]],components=1,nrep=10,scatter=T)
fit64[[3]] <- mvnmix(dat=uk64.split[[6]],components=3,nrep=10,scatter=T)

show.marginals(fit64,numdraws=50000)

sr64 <- swingratio(fit64,sims=10000)

##########
## 1966 ##
##########

uk66 <- UK.elections$'1966'
uk66 <- data.frame(Total=uk66$Total,
                   Labour=uk66$vsh_lab,
                   Conservative=uk66$vsh_cons,
                   Liberal=uk66$vsh_lib,
				   Regional=uk66$vsh_rep_lab)

uk66.split <- findpatterns(uk66)

fit66 <- list()
fit66[[1]] <- mvnmix(dat=uk66.split[[6]],components=1,nrep=10,scatter=T)
fit66[[2]] <- mvnmix(dat=uk66.split[[7]],components=2,nrep=10,scatter=T)
fit66[[3]] <- mvnmix(dat=uk66.split[[8]],components=3,nrep=10,scatter=T)

show.marginals(fit66,numdraws=50000)

sr66 <- swingratio(fit66,sims=10000)

##########
## 1970 ##
##########

uk70 <- UK.elections$'1970'
uk70 <- data.frame(Total=uk70$Total,
                   Labour=uk70$vsh_lab,
                   Conservative=uk70$vsh_cons,
                   Liberal=uk70$vsh_lib,
				   
				   Regional=uk70$vsh_snp+uk70$vsh_unity+uk70$vsh_prot_un+
				   uk70$vsh_rep_lab)

uk70.split <- findpatterns(uk70)

fit70 <- list()
fit70[[1]] <- mvnmix(dat=uk70.split[[6]],components=2,nrep=10,scatter=T)
fit70[[2]] <- mvnmix(dat=uk70.split[[7]],components=2,nrep=10,scatter=T)
fit70[[3]] <- mvnmix(dat=uk70.split[[8]],components=3,nrep=10,scatter=T)
fit70[[4]] <- mvnmix(dat=uk70.split[[9]],components=4,nrep=10,scatter=T)

show.marginals(fit70,numdraws=50000)

sr70 <- swingratio(fit70,sims=10000)

###########
## 19741 ##
###########

# NOTE: We dropped vsh_dem_lab from the analysis, despite them getting a couple of seats. The Ulster Unionists and the SDLP, despite winning seats, don't appear in the data.

uk741 <- UK.elections$'19741'
uk741 <- data.frame(Total=uk741$Total,
                   Labour=uk741$vsh_lab,
                   Conservative=uk741$vsh_cons,
                   Liberal=uk741$vsh_lib,
				   
				   Regional=uk741$vsh_snp+
				   uk741$vsh_pl_cymru+uk741$vsh_vanguard+
				   uk741$vsh_dem_un)

uk741.split <- findpatterns(uk741)

fit741 <- list()
fit741[[1]] <- mvnmix(dat=uk741.split[[3]],components=1,nrep=10,scatter=T)
fit741[[2]] <- mvnmix(dat=uk741.split[[4]],components=3,nrep=10,scatter=T)
fit741[[3]] <- mvnmix(dat=uk741.split[[5]],components=3,nrep=10,scatter=T)
fit741[[4]] <- mvnmix(dat=uk741.split[[6]],components=1,nrep=10,scatter=T)

show.marginals(fit741,numdraws=50000)

sr741 <- swingratio(fit741,sims=10000)

###########
## 19742 ##
###########

# NOTE: The SDLP, despite winning seats, doesn't appear in the data.

uk742 <- UK.elections$'19742'
uk742 <- data.frame(Total=uk742$Total,
                   Labour=uk742$vsh_lab,
                   Conservative=uk742$vsh_cons,
                   Liberal=uk742$vsh_lib,
				   
				   Regional=uk742$vsh_snp+uk742$vsh_ulster_un+
                   uk742$vsh_pl_cymru+uk742$vsh_vanguard+
                   uk742$vsh_dem_un)

uk742.split <- findpatterns(uk742)

fit742 <- list()
fit742[[1]] <- mvnmix(dat=uk742.split[[5]],components=3,nrep=10,scatter=T)
fit742[[2]] <- mvnmix(dat=uk742.split[[6]],components=3,nrep=10,scatter=T)

show.marginals(fit742,numdraws=50000)

sr742 <- swingratio(fit742,sims=10000)

##########
## 1979 ##
##########

# NOTE: uk79$vsh_ulster_pop_un is not included.

uk79 <- UK.elections$'1979'
uk79 <- data.frame(Total=uk79$Total,
                   Labour=uk79$vsh_lab,
                   Conservative=uk79$vsh_cons,
                   Liberal=uk79$vsh_lib,
				   
				   Regional=uk79$vsh_snp+uk79$vsh_ulster_un+
				   uk79$vsh_dem_un+uk79$vsh_sdlp+
                   uk79$vsh_pl_cymru+uk79$vsh_un_ulster_un)

uk79.split <- findpatterns(uk79)

fit79 <- list()
fit79[[1]] <- mvnmix(dat=uk79.split[[3]],components=1,nrep=10,scatter=T)
fit79[[2]] <- mvnmix(dat=uk79.split[[4]],components=1,nrep=10,scatter=T)
fit79[[3]] <- mvnmix(dat=uk79.split[[5]],components=2,nrep=10,scatter=T)
fit79[[4]] <- mvnmix(dat=uk79.split[[6]],components=4,nrep=10,scatter=T)
fit79[[5]] <- mvnmix(dat=uk79.split[[7]],components=3,nrep=10,scatter=T)
fit79[[6]] <- mvnmix(dat=uk79.split[[8]],components=2,nrep=10,scatter=T)

show.marginals(fit79,numdraws=50000)

sr79 <- swingratio(fit79,sims=10000)

##########
## 1983 ##
##########

uk83 <- UK.elections$'1983'
uk83 <- data.frame(Total=uk83$Total,
                   Labour=uk83$vsh_lab,
                   Conservative=uk83$vsh_cons,
                   Alliance=uk83$vsh_lib+uk83$vsh_soc_dem,
				   
				   Regional=uk83$vsh_snp+uk83$vsh_ulster_un+
				   uk83$vsh_dem_un+uk83$vsh_soc_dem_lab+
                   uk83$vsh_pl_cymru+uk83$vsh_sinnfein+uk83$vsh_alliance+
                   uk83$vsh_ulster_pop_un)

uk83.split <- findpatterns(uk83)

fit83 <- list()
fit83[[1]] <- mvnmix(dat=uk83.split[[2]],components=1,nrep=10,scatter=T)
fit83[[2]] <- mvnmix(dat=uk83.split[[5]],components=3,nrep=10,scatter=T)
fit83[[3]] <- mvnmix(dat=uk83.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit83,numdraws=50000)

sr83 <- swingratio(fit83,sims=10000)

##########
## 1987 ##
##########

uk87 <- UK.elections$'1987'
uk87 <- data.frame(Total=uk87$Total,
                   Labour=uk87$vsh_lab,
                   Conservative=uk87$vsh_cons,
                   Alliance=uk87$vsh_lib+uk87$vsh_soc_dem,
                   Regional=uk87$vsh_snp+uk87$vsh_ulster_un+
                            uk87$vsh_soc_dem_lab+uk87$vsh_pl_cymru+
                            uk87$vsh_dem_un+uk87$vsh_sinnfein+uk87$vsh_alliance)

uk87.split <- findpatterns(uk87)

fit87 <- list()
fit87[[1]] <- mvnmix(dat=uk87.split[[2]],components=3,nrep=10,scatter=T)
fit87[[2]] <- mvnmix(dat=uk87.split[[4]],components=3,nrep=10,scatter=T)
fit87[[3]] <- mvnmix(dat=uk87.split[[5]],components=3,nrep=10,scatter=T)

show.marginals(fit87,numdraws=50000)

sr87 <- swingratio(fit87,sims=10000)

##########
## 1992 ##
##########

uk92 <- UK.elections$'1992'
uk92 <- data.frame(Total=uk92$Total,
                   Labour=uk92$vsh_lab,
                   Conservative=uk92$vsh_cons,
                   LibDem=uk92$vsh_libdem,
                   Regional=uk92$vsh_snp+uk92$vsh_ulster_un+
                            uk92$vsh_soc_dem_lab+uk92$vsh_pl_cymru+
                            uk92$vsh_dem_un+uk92$vsh_sinnfein+uk92$vsh_alliance)

uk92.split <- findpatterns(uk92)

fit92 <- list()
fit92[[1]] <- mvnmix(dat=uk92.split[[2]],components=3,nrep=20,scatter=T)
fit92[[2]] <- mvnmix(dat=uk92.split[[3]],components=1,nrep=10,scatter=T)
fit92[[3]] <- mvnmix(dat=uk92.split[[4]],components=4,nrep=10,scatter=T)
fit92[[4]] <- mvnmix(dat=uk92.split[[5]],components=3,nrep=10,scatter=T)

show.marginals(fit92,numdraws=50000)

sr92 <- swingratio(fit92,sims=10000)

##########
## 1997 ##
##########

uk97 <- UK.elections$'1997'
uk97 <- data.frame(Total=uk97$Total,
                   Labour=uk97$vsh_lab,
                   Conservative=uk97$vsh_cons,
                   LibDem=uk97$vsh_libdem,
                   Regional=uk97$vsh_snp+uk97$vsh_ulster_un+
                            uk97$vsh_soc_dem_lab+uk97$vsh_pl_cymru+
                            uk97$vsh_dem_un+uk97$vsh_sinnfein)
uk97.split <- findpatterns(uk97)

fit97 <- list()
fit97[[1]] <- mvnmix(dat=uk97.split[[4]],components=2,nrep=10,scatter=T)
fit97[[2]] <- mvnmix(dat=uk97.split[[5]],components=2,nrep=10,scatter=T)
fit97[[3]] <- mvnmix(dat=uk97.split[[6]],components=2,nrep=10,scatter=T)
fit97[[4]] <- mvnmix(dat=uk97.split[[7]],components=3,nrep=10,scatter=T)
fit97[[5]] <- mvnmix(dat=uk97.split[[8]],components=3,nrep=10,scatter=T)

show.marginals(fit97,numdraws=50000)

sr97 <- swingratio(fit97,sims=10000)

##########
## 2001 ##
##########

uk01 <- UK.elections$'2001'
uk01 <- data.frame(Total=uk01$Total,
                   Labour=uk01$vsh_lab,
                   Conservative=uk01$vsh_cons,
                   LibDem=uk01$vsh_libdem,
                   Regional=uk01$vsh_snp+uk01$vsh_ulster_un+
                            uk01$vsh_soc_dem_lab+uk01$vsh_pl_cymru+
							uk01$vsh_dem_un+uk01$vsh_sinnfein)
uk01.split <- findpatterns(uk01)

fit01 <- list()
fit01[[1]] <- mvnmix(dat=uk01.split[[2]],components=1,nrep=10,scatter=T)
fit01[[2]] <- mvnmix(dat=uk01.split[[3]],components=4,nrep=10,scatter=T)
fit01[[3]] <- mvnmix(dat=uk01.split[[4]],components=2,nrep=10,scatter=T)
fit01[[4]] <- mvnmix(dat=uk01.split[[6]],components=4,nrep=10,scatter=T)
fit01[[5]] <- mvnmix(dat=uk01.split[[7]],components=2,nrep=10,scatter=T)

show.marginals(fit01,numdraws=50000)

sr01 <- swingratio(fit01,sims=10000)

##########
## 2005 ##
##########

# NOTE: uk05$vsh_soc_dem_lab is not included.

uk05 <- UK.elections$'2005'
uk05 <- data.frame(Total=uk05$Total,
                   Labour=uk05$vsh_lab,
                   Conservative=uk05$vsh_cons,
                   LibDem=uk05$vsh_libdem,
                   Regional=uk05$vsh_snp+uk05$vsh_ulster_un+
                            uk05$vsh_pl_cymru+
							uk05$vsh_dem_un+uk05$vsh_sinnfein)
uk05.split <- findpatterns(uk05)

fit05 <- list()
fit05[[1]] <- mvnmix(dat=uk05.split[[2]],components=1,nrep=10,scatter=T)
fit05[[2]] <- mvnmix(dat=uk05.split[[4]],components=4,nrep=10,scatter=T)
fit05[[3]] <- mvnmix(dat=uk05.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit05,numdraws=50000)

sr05 <- swingratio(fit05,sims=10000)


# Assemble results (see Linzer's seatsvotes code).
UKresults <- list(sr45,sr50,sr51,sr55,sr59,sr64,sr66,sr70,sr741,sr742,sr79,sr83,sr87,sr92,sr97,sr01,sr05)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(UKresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(UKresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(UKresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(UKresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(UKresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]


#########################
##### United States #####
#########################

# set random number seed to guarantee perfect replicability
set.seed(1234)

##########
## 1946 ##
##########

us46 <- US.elections$'1946'

us46 <- data.frame(Total=us46$Total,
                   Democrat=us46$vsh_dem,
                   Republican=us46$vsh_rep,
                   
                   Other=us46$vsh_am_lab+us46$vsh_dem_frm_lab+
                   us46$vsh_lib+us46$vsh_prohib+us46$vsh_soc+us46$vsh_un_cit+
                   us46$vsh_vet_vic+us46$vsh_indep+us46$vsh_others)

us46.split <- findpatterns(us46)

fit46 <- list()
fit46[[1]] <- mvnmix(dat=us46.split[[1]],components=2,nrep=10,scatter=T)
fit46[[2]] <- mvnmix(dat=us46.split[[2]],components=2,nrep=10,scatter=T)
fit46[[3]] <- mvnmix(dat=us46.split[[3]],components=3,nrep=10,scatter=T)
fit46[[4]] <- mvnmix(dat=us46.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit46,numdraws=50000)

sr46 <- swingratio(fit46,sims=10000)

##########
## 1948 ##
##########

us48 <- US.elections$'1948'

us48 <- data.frame(Total=us48$Total,
                   Democrat=us48$vsh_dem,
                   Republican=us48$vsh_rep,  
                   
                   Other=us48$vsh_am_lab+us48$vsh_dem_frm_lab+
                   us48$vsh_ind_prog+us48$vsh_lib+us48$vsh_prog+
                   us48$vsh_indep+us48$vsh_others)

us48.split <- findpatterns(us48)

fit48 <- list()
fit48[[1]] <- mvnmix(dat=us48.split[[1]],components=3,nrep=10,scatter=T)
fit48[[2]] <- mvnmix(dat=us48.split[[2]],components=3,nrep=10,scatter=T)
fit48[[3]] <- mvnmix(dat=us48.split[[3]],components=1,nrep=10,scatter=T)
fit48[[4]] <- mvnmix(dat=us48.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit48,numdraws=50000)

sr48 <- swingratio(fit48,sims=10000)

##########
## 1950 ##
##########

us50 <- US.elections$'1950'

us50 <- data.frame(Total=us50$Total,
                   Democrat=us50$vsh_dem,
                   Republican=us50$vsh_rep,
                   
                   Other=us50$vsh_am_lab+us50$vsh_dem_frm_lab+
                   us50$vsh_ind_prog+us50$vsh_lib+us50$vsh_prog+
                   us50$vsh_indep+us50$vsh_others)

us50.split <- findpatterns(us50)

fit50 <- list()
fit50[[1]] <- mvnmix(dat=us50.split[[1]],components=2,nrep=10,scatter=T)
fit50[[2]] <- mvnmix(dat=us50.split[[2]],components=3,nrep=10,scatter=T)
fit50[[3]] <- mvnmix(dat=us50.split[[3]],components=3,nrep=10,scatter=T)
fit50[[4]] <- mvnmix(dat=us50.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit50,numdraws=50000)

sr50 <- swingratio(fit50,sims=10000)

##########
## 1952 ##
##########

us52 <- US.elections$'1952'

us52 <- data.frame(Total=us52$Total,
                   Democrat=us52$vsh_dem,
                   Republican=us52$vsh_rep,
                   
                   Other=us52$vsh_am_lab+us52$vsh_dem_frm_lab+
                   us52$vsh_ind_prog+us52$vsh_lib+us52$vsh_indep
                   +us52$vsh_others)

us52.split <- findpatterns(us52)

fit52 <- list()
fit52[[1]] <- mvnmix(dat=us52.split[[1]],components=3,nrep=10,scatter=T)
fit52[[2]] <- mvnmix(dat=us52.split[[2]],components=2,nrep=10,scatter=T)
fit52[[3]] <- mvnmix(dat=us52.split[[3]],components=3,nrep=10,scatter=T)
fit52[[4]] <- mvnmix(dat=us52.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit52,numdraws=50000)

sr52 <- swingratio(fit52,sims=10000)

##########
## 1954 ##
##########

us54 <- US.elections$'1954'

us54 <- data.frame(Total=us54$Total,
                   Democrat=us54$vsh_dem,
                   Republican=us54$vsh_rep,
                   
                   Other=us54$vsh_dem_frm_lab+us54$vsh_lib+
                   us54$vsh_indep+us54$vsh_others)

us54.split <- findpatterns(us54)

fit54 <- list()
fit54[[1]] <- mvnmix(dat=us54.split[[1]],components=2,nrep=10,scatter=T)
fit54[[2]] <- mvnmix(dat=us54.split[[2]],components=3,nrep=10,scatter=T)
fit54[[3]] <- mvnmix(dat=us54.split[[3]],components=4,nrep=10,scatter=T)
fit54[[4]] <- mvnmix(dat=us54.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit54,numdraws=50000)

sr54 <- swingratio(fit54,sims=10000)

##########
## 1956 ##
##########

us56 <- US.elections$'1956'

us56 <- data.frame(Total=us56$Total,
                   Democrat=us56$vsh_dem,
                   Republican=us56$vsh_rep,
                   
                   Other=us56$vsh_dem_frm_lab+us56$vsh_lib+
                   us56$vsh_indep+us56$vsh_others)

us56.split <- findpatterns(us56)

fit56 <- list()
fit56[[1]] <- mvnmix(dat=us56.split[[1]],components=2,nrep=10,scatter=T)
fit56[[2]] <- mvnmix(dat=us56.split[[2]],components=2,nrep=10,scatter=T)
fit56[[3]] <- mvnmix(dat=us56.split[[3]],components=4,nrep=10,scatter=T)
fit56[[4]] <- mvnmix(dat=us56.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit56,numdraws=50000)

sr56 <- swingratio(fit56,sims=10000)

##########
## 1958 ##
##########

us58 <- US.elections$'1958'

us58 <- data.frame(Total=us58$Total,
                   Democrat=us58$vsh_dem,
                   Republican=us58$vsh_rep,
                   
                   Other=us58$vsh_constl+us58$vsh_dem_frm_lab+
                   us58$vsh_free_dem+us58$vsh_ind_demcy+us58$vsh_lib+
                   us58$vsh_indep+us58$vsh_others)

us58.split <- findpatterns(us58)

fit58 <- list()
fit58[[1]] <- mvnmix(dat=us58.split[[1]],components=2,nrep=10,scatter=T)
fit58[[2]] <- mvnmix(dat=us58.split[[2]],components=3,nrep=10,scatter=T)
fit58[[3]] <- mvnmix(dat=us58.split[[3]],components=3,nrep=10,scatter=T)
fit58[[4]] <- mvnmix(dat=us58.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit58,numdraws=50000)

sr58 <- swingratio(fit58,sims=10000)

##########
## 1960 ##
##########

us60 <- US.elections$'1960'

us60 <- data.frame(Total=us60$Total,
                   Democrat=us60$vsh_dem,
                   Republican=us60$vsh_rep,
                   
                   Other=us60$vsh_constl+us60$vsh_dem_frm_lab+
                   us60$vsh_lib+us60$vsh_soc_dem+us60$vsh_indep+
                   us60$vsh_others)

us60.split <- findpatterns(us60)

fit60 <- list()
fit60[[1]] <- mvnmix(dat=us60.split[[1]],components=3,nrep=10,scatter=T)
fit60[[2]] <- mvnmix(dat=us60.split[[2]],components=2,nrep=10,scatter=T)
fit60[[3]] <- mvnmix(dat=us60.split[[3]],components=3,nrep=10,scatter=T)
fit60[[4]] <- mvnmix(dat=us60.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit60,numdraws=50000)

sr60 <- swingratio(fit60,sims=10000)

##########
## 1962 ##
##########

us62 <- US.elections$'1962'

us62 <- data.frame(Total=us62$Total,
                   Democrat=us62$vsh_dem,
                   Republican=us62$vsh_rep,
                   
                   Other=us62$vsh_dem_frm_lab+us62$vsh_ind_demcy+
                   us62$vsh_lib+us62$vsh_prohib+us62$vsh_indep+
                   us62$vsh_others)

us62.split <- findpatterns(us62)

fit62 <- list()
fit62[[1]] <- mvnmix(dat=us62.split[[1]],components=1,nrep=10,scatter=T)
fit62[[2]] <- mvnmix(dat=us62.split[[2]],components=3,nrep=10,scatter=T)
fit62[[3]] <- mvnmix(dat=us62.split[[3]],components=3,nrep=10,scatter=T)
fit62[[4]] <- mvnmix(dat=us62.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit62,numdraws=50000)

sr62 <- swingratio(fit62,sims=10000)

##########
## 1964 ##
##########

us64 <- US.elections$'1964'

us64 <- data.frame(Total=us64$Total,
                   Democrat=us64$vsh_dem,
                   Republican=us64$vsh_rep,
                   
                   Other=us64$vsh_dem_frm_lab+us64$vsh_lib+
                   us64$vsh_indep+us64$vsh_others)

us64.split <- findpatterns(us64)

fit64 <- list()
fit64[[1]] <- mvnmix(dat=us64.split[[2]],components=1,nrep=10,scatter=T)
fit64[[2]] <- mvnmix(dat=us64.split[[3]],components=3,nrep=10,scatter=T)
fit64[[3]] <- mvnmix(dat=us64.split[[4]],components=3,nrep=10,scatter=T)
fit64[[4]] <- mvnmix(dat=us64.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit64,numdraws=50000)

sr64 <- swingratio(fit64,sims=10000)

##########
## 1966 ##
##########

us66 <- US.elections$'1966'

us66 <- data.frame(Total=us66$Total,
                   Democrat=us66$vsh_dem,
                   Republican=us66$vsh_rep,
                   
                   Other=us66$vsh_am_ind+us66$vsh_cons+
                   us66$vsh_const+us66$vsh_dem_frm_lab+
                   us66$vsh_lib+us66$vsh_indep+us66$vsh_others)

us66.split <- findpatterns(us66)

fit66 <- list()
fit66[[1]] <- mvnmix(dat=us66.split[[1]],components=2,nrep=10,scatter=T)
fit66[[2]] <- mvnmix(dat=us66.split[[2]],components=2,nrep=10,scatter=T)
fit66[[3]] <- mvnmix(dat=us66.split[[3]],components=3,nrep=10,scatter=T)
fit66[[4]] <- mvnmix(dat=us66.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit66,numdraws=50000)

sr66 <- swingratio(fit66,sims=10000)

##########
## 1968 ##
##########

us68 <- US.elections$'1968'

us68 <- data.frame(Total=us68$Total,
                   Democrat=us68$vsh_dem,
                   Republican=us68$vsh_rep,
                   
                   Other=us68$vsh_cons+us68$vsh_dem_frm_lab
                   +us68$vsh_denv_ind+us68$vsh_lib+
                   us68$vsh_nat_dem_ala+us68$vsh_pce_fr+
                   us68$vsh_vce_ind+us68$vsh_indep+us68$vsh_others)

us68.split <- findpatterns(us68)

fit68 <- list()
fit68[[1]] <- mvnmix(dat=us68.split[[1]],components=2,nrep=10,scatter=T)
fit68[[2]] <- mvnmix(dat=us68.split[[2]],components=3,nrep=10,scatter=T)
fit68[[3]] <- mvnmix(dat=us68.split[[3]],components=2,nrep=10,scatter=T)
fit68[[4]] <- mvnmix(dat=us68.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit68,numdraws=50000)

sr68 <- swingratio(fit68,sims=10000)

##########
## 1970 ##
##########

us70 <- US.elections$'1970'

us70 <- data.frame(Total=us70$Total,
                   Democrat=us70$vsh_dem,
                   Republican=us70$vsh_rep,
                   
                   Other=us70$vsh_am+us70$vsh_am_miss+
                   us70$vsh_cons+us70$vsh_dem_frm_lab+
                   us70$vsh_lib+us70$vsh_nat_dem_ala+
                   us70$vsh_indep+us70$vsh_others)

us70.split <- findpatterns(us70)

fit70 <- list()
fit70[[1]] <- mvnmix(dat=us70.split[[1]],components=3,nrep=10,scatter=T)
fit70[[2]] <- mvnmix(dat=us70.split[[2]],components=4,nrep=10,scatter=T)
fit70[[3]] <- mvnmix(dat=us70.split[[3]],components=3,nrep=10,scatter=T)
fit70[[4]] <- mvnmix(dat=us70.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit70,numdraws=50000)

sr70 <- swingratio(fit70,sims=10000)

##########
## 1972 ##
##########

us72 <- US.elections$'1972'

us72 <- data.frame(Total=us72$Total,
                   Democrat=us72$vsh_dem,
                   Republican=us72$vsh_rep,
                   
                   Other=us72$vsh_act+us72$vsh_am+
                   us72$vsh_am_ind+us72$vsh_cons+
                   us72$vsh_dc+us72$vsh_dem_frm_lab+
                   us72$vsh_lib+us72$vsh_minn_tax+
                   us72$vsh_nat_dem_ala+us72$vsh_pce_fr+
                   us72$vsh_soc_work+us72$vsh_indep
                   +us72$vsh_others)

us72.split <- findpatterns(us72)

fit72 <- list()
fit72[[1]] <- mvnmix(dat=us72.split[[1]],components=3,nrep=10,scatter=T)
fit72[[2]] <- mvnmix(dat=us72.split[[2]],components=2,nrep=10,scatter=T)
fit72[[3]] <- mvnmix(dat=us72.split[[3]],components=1,nrep=10,scatter=T)
fit72[[4]] <- mvnmix(dat=us72.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit72,numdraws=50000)

sr72 <- swingratio(fit72,sims=10000)

##########
## 1974 ##
##########

us74 <- US.elections$'1974'

us74 <- data.frame(Total=us74$Total,
                   Democrat=us74$vsh_dem,
                   Republican=us74$vsh_rep,
                   
                   Other=us74$vsh_ala_cons+us74$vsh_am+
                   us74$vsh_am_ind+us74$vsh_communist+
                   us74$vsh_cons+us74$vsh_dem_frm_lab+
                   us74$vsh_econ_just+us74$vsh_lib+
                   us74$vsh_liby_un+us74$vsh_llj+
                   us74$vsh_us_lab+us74$vsh_indep+
                   us74$vsh_others)

us74.split <- findpatterns(us74)

fit74 <- list()
fit74[[1]] <- mvnmix(dat=us74.split[[1]],components=2,nrep=10,scatter=T)
fit74[[2]] <- mvnmix(dat=us74.split[[2]],components=3,nrep=10,scatter=T)
fit74[[3]] <- mvnmix(dat=us74.split[[3]],components=1,nrep=10,scatter=T)
fit74[[4]] <- mvnmix(dat=us74.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit74,numdraws=50000)

sr74 <- swingratio(fit74,sims=10000)

##########
## 1976 ##
##########

us76 <- US.elections$'1976'

us76 <- data.frame(Total=us76$Total,
                   Democrat=us76$vsh_dem,
                   Republican=us76$vsh_rep,
                   
                   Other=us76$vsh_am+us76$vsh_cons+
                   us76$vsh_dem_frm_lab+us76$vsh_lib+
                   us76$vsh_pce_fr+us76$vsh_sl_maj+
                   us76$vsh_us_lab+us76$vsh_indep+
                   us76$vsh_others)

us76.split <- findpatterns(us76)

fit76 <- list()
fit76[[1]] <- mvnmix(dat=us76.split[[3]],components=1,nrep=10,scatter=T)
fit76[[2]] <- mvnmix(dat=us76.split[[4]],components=3,nrep=10,scatter=T)
fit76[[3]] <- mvnmix(dat=us76.split[[5]],components=3,nrep=10,scatter=T)
fit76[[4]] <- mvnmix(dat=us76.split[[6]],components=2,nrep=10,scatter=T)

show.marginals(fit76,numdraws=50000)

sr76 <- swingratio(fit76,sims=10000)

##########
## 1978 ##
##########

us78 <- US.elections$'1978'

us78 <- data.frame(Total=us78$Total,
                   Democrat=us78$vsh_dem,
                   Republican=us78$vsh_rep,
                   
                   Other=us78$vsh_alw_ppl+us78$vsh_am_ind+
                   us78$vsh_am_minn+us78$vsh_cons+
                   us78$vsh_dem_frm_lab+us78$vsh_ra_un+
                   us78$vsh_lib+us78$vsh_libtn+
                   us78$vsh_liby_un+us78$vsh_soc_iowa+
                   us78$vsh_us_lab+us78$vsh_indep+us78$vsh_others)

us78.split <- findpatterns(us78)

fit78 <- list()
fit78[[1]] <- mvnmix(dat=us78.split[[2]],components=3,nrep=10,scatter=T)
fit78[[2]] <- mvnmix(dat=us78.split[[3]],components=2,nrep=10,scatter=T)
fit78[[3]] <- mvnmix(dat=us78.split[[4]],components=1,nrep=10,scatter=T)
fit78[[4]] <- mvnmix(dat=us78.split[[5]],components=2,nrep=10,scatter=T)

show.marginals(fit78,numdraws=50000)

sr78 <- swingratio(fit78,sims=10000)

##########
## 1980 ##
##########

us80 <- US.elections$'1980'

us80 <- data.frame(Total=us80$Total,
                   Democrat=us80$vsh_dem,
                   Republican=us80$vsh_rep,
                   
                   Other=us80$vsh_ad_libtn+
                   us80$vsh_ala_libtn+us80$vsh_cit+
                   us80$vsh_cons+us80$vsh_dem_frm_lab+
                   us80$vsh_lib+us80$vsh_libtn+
                   us80$vsh_liby_un+us80$vsh_mid_cla+
                   us80$vsh_pce_fr+us80$vsh_rgt_life+
                   us80$vsh_indep+us80$vsh_others)

us80.split <- findpatterns(us80)

fit80 <- list()
fit80[[1]] <- mvnmix(dat=us80.split[[1]],components=2,nrep=10,scatter=T)
fit80[[2]] <- mvnmix(dat=us80.split[[2]],components=3,nrep=10,scatter=T)
fit80[[3]] <- mvnmix(dat=us80.split[[3]],components=3,nrep=10,scatter=T)
fit80[[4]] <- mvnmix(dat=us80.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit80,numdraws=50000)

sr80 <- swingratio(fit80,sims=10000)

##########
## 1982 ##
##########

us82 <- US.elections$'1982'

us82 <- data.frame(Total=us82$Total,
                   Democrat=us82$vsh_dem,
                   Republican=us82$vsh_rep,
                   
                   Other=us82$vsh_cons+
                   us82$vsh_dem_frm_lab+us82$vsh_lib+
                   us82$vsh_libtn+us82$vsh_mil_str+
                   us82$vsh_indep+us82$vsh_others)

us82.split <- findpatterns(us82)

fit82 <- list()
fit82[[1]] <- mvnmix(dat=us82.split[[1]],components=2,nrep=10,scatter=T)
fit82[[2]] <- mvnmix(dat=us82.split[[2]],components=2,nrep=10,scatter=T)
fit82[[3]] <- mvnmix(dat=us82.split[[3]],components=2,nrep=10,scatter=T)
fit82[[4]] <- mvnmix(dat=us82.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit82,numdraws=50000)

sr82 <- swingratio(fit82,sims=10000)

##########
## 1984 ##
##########

us84 <- US.elections$'1984'

us84 <- data.frame(Total=us84$Total,
                   Democrat=us84$vsh_dem,
                   Republican=us84$vsh_rep,
                   
                   Other=us84$vsh_communist+
                   us84$vsh_ccs_ppl+us84$vsh_cons+
                   us84$vsh_lib+us84$vsh_libtn+
                   us84$vsh_mid_cla+us84$vsh_pce_fr+
                   us84$vsh_ppl_prof+us84$vsh_ratep_ag_lilco+
                   us84$vsh_indep+us84$vsh_others)

us84.split <- findpatterns(us84)

fit84 <- list()
fit84[[1]] <- mvnmix(dat=us84.split[[1]],components=2,nrep=10,scatter=T)
fit84[[2]] <- mvnmix(dat=us84.split[[2]],components=1,nrep=10,scatter=T)
fit84[[3]] <- mvnmix(dat=us84.split[[3]],components=4,nrep=10,scatter=T)
fit84[[4]] <- mvnmix(dat=us84.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit84,numdraws=50000)

sr84 <- swingratio(fit84,sims=10000)

##########
## 1986 ##
##########

us86 <- US.elections$'1986'

us86 <- data.frame(Total=us86$Total,
                   Democrat=us86$vsh_dem,
                   Republican=us86$vsh_rep,
                   
                   Other=us86$vsh_cons+us86$vsh_dem_frm_lab+
                   us86$vsh_lib+us86$vsh_libtn+us86$vsh_pce_fr+
                   us86$vsh_indep+us86$vsh_others)

us86.split <- findpatterns(us86)

fit86 <- list()
fit86[[1]] <- mvnmix(dat=us86.split[[1]],components=4,nrep=10,scatter=T)
fit86[[2]] <- mvnmix(dat=us86.split[[2]],components=2,nrep=10,scatter=T)
fit86[[3]] <- mvnmix(dat=us86.split[[3]],components=2,nrep=10,scatter=T)
fit86[[4]] <- mvnmix(dat=us86.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit86,numdraws=50000)

sr86 <- swingratio(fit86,sims=10000)

##########
## 1988 ##
##########

us88 <- US.elections$'1988'

us88 <- data.frame(Total=us88$Total,
                   Democrat=us88$vsh_dem,
                   Republican=us88$vsh_rep,
                   
                   Other=us88$vsh_cons+us88$vsh_dem_frm_lab+
                   us88$vsh_lib+us88$vsh_libtn+us88$vsh_pce_fr+
                   us88$vsh_rgt_life+us88$vsh_indep+us88$vsh_others)

us88.split <- findpatterns(us88)

fit88 <- list()
fit88[[1]] <- mvnmix(dat=us88.split[[1]],components=2,nrep=10,scatter=T)
fit88[[2]] <- mvnmix(dat=us88.split[[2]],components=2,nrep=10,scatter=T)
fit88[[3]] <- mvnmix(dat=us88.split[[3]],components=3,nrep=10,scatter=T)
fit88[[4]] <- mvnmix(dat=us88.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit88,numdraws=50000)

sr88 <- swingratio(fit88,sims=10000)

##########
## 1990 ##
##########

us90 <- US.elections$'1990'

us90 <- data.frame(Total=us90$Total,
                   Democrat=us90$vsh_dem,
                   Republican=us90$vsh_rep,
                   
                   Other=us90$vsh_cons+us90$vsh_dc+
                   us90$vsh_dem_frm_lab+us90$vsh_ill_sol+
                   us90$vsh_jim_wham+us90$vsh_lib+
                   us90$vsh_libtn+us90$vsh_new_all+
                   us90$vsh_pce_fr+us90$vsh_pop+us90$vsh_rgt_life+
                   us90$vsh_indep+us90$vsh_others)

us90.split <- findpatterns(us90)

fit90 <- list()
fit90[[1]] <- mvnmix(dat=us90.split[[1]],components=2,nrep=10,scatter=T)
fit90[[2]] <- mvnmix(dat=us90.split[[2]],components=4,nrep=10,scatter=T)
fit90[[3]] <- mvnmix(dat=us90.split[[3]],components=1,nrep=10,scatter=T)
fit90[[4]] <- mvnmix(dat=us90.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit90,numdraws=50000)

sr90 <- swingratio(fit90,sims=10000)

##########
## 1992 ##
##########

us92 <- US.elections$'1992'

us92 <- data.frame(Total=us92$Total,
                   Democrat=us92$vsh_dem,
                   Republican=us92$vsh_rep,
                   
                   Other=us92$vsh_conn_pty+us92$vsh_alk_ind+
                   us92$vsh_am_gra+us92$vsh_cons+us92$vsh_dem_frm_lab+
                   us92$vsh_econ_rec+us92$vsh_for_ppl+us92$vsh_green+
                   us92$vsh_ind_chg+us92$vsh_ind_vot+us92$vsh_ind_perot+
                   us92$vsh_libtn+us92$vsh_lou_pet+us92$vsh_pce_fr+
                   us92$vsh_perot_chc+us92$vsh_indep+us92$vsh_others)

us92.split <- findpatterns(us92)

fit92 <- list()
fit92[[1]] <- mvnmix(dat=us92.split[[1]],components=4,nrep=10,scatter=T)
fit92[[2]] <- mvnmix(dat=us92.split[[2]],components=4,nrep=10,scatter=T)
fit92[[3]] <- mvnmix(dat=us92.split[[3]],components=1,nrep=10,scatter=T)
fit92[[4]] <- mvnmix(dat=us92.split[[4]],components=2,nrep=10,scatter=T)

show.marginals(fit92,numdraws=50000)

sr92 <- swingratio(fit92,sims=10000)

##########
## 1994 ##
##########

us94 <- US.elections$'1994'

us94 <- data.frame(Total=us94$Total,
                   Democrat=us94$vsh_dem,
                   Republican=us94$vsh_rep,
                   
                   Other=us94$vsh_conn_pty+us94$vsh_am_ind+us94$vsh_cons+
                   us94$vsh_dem_frm_lab+us94$vsh_green+us94$vsh_libtn+
                   us94$vsh_miss_taxp+us94$vsh_nat_law+us94$vsh_pce_fr+
                   us94$vsh_rgt_life+us94$vsh_taxp+us94$vsh_indep+us94$vsh_others)

us94.split <- findpatterns(us94)

fit94 <- list()
fit94[[1]] <- mvnmix(dat=us94.split[[1]],components=2,nrep=10,scatter=T)
fit94[[2]] <- mvnmix(dat=us94.split[[2]],components=2,nrep=10,scatter=T)
fit94[[3]] <- mvnmix(dat=us94.split[[3]],components=1,nrep=10,scatter=T)
fit94[[4]] <- mvnmix(dat=us94.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit94,numdraws=50000)

sr94 <- swingratio(fit94,sims=10000)

##########
## 1996 ##
##########

us96 <- US.elections$'1996'

us96 <- data.frame(Total=us96$Total,
                   Democrat=us96$vsh_dem,
                   Republican=us96$vsh_rep,
                   
                   Other=us96$vsh_conn_pty+us96$vsh_cons+us96$vsh_dem_frm_lab+
                   us96$vsh_gra_rts+us96$vsh_lib+us96$vsh_libtn+us96$vsh_nat_law+
                   us96$vsh_ref+us96$vsh_indep+us96$vsh_others)

us96.split <- findpatterns(us96)

fit96 <- list()
fit96[[1]] <- mvnmix(dat=us96.split[[1]],components=3,nrep=10,scatter=T)
fit96[[2]] <- mvnmix(dat=us96.split[[2]],components=1,nrep=10,scatter=T)
fit96[[3]] <- mvnmix(dat=us96.split[[3]],components=1,nrep=10,scatter=T)
fit96[[4]] <- mvnmix(dat=us96.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit96,numdraws=50000)

sr96 <- swingratio(fit96,sims=10000)

##########
## 1998 ##
##########

us98 <- US.elections$'1998'

us98 <- data.frame(Total=us98$Total,
                   Democrat=us98$vsh_dem,
                   Republican=us98$vsh_rep,
                   
                   Other=us98$vsh_am_her+us98$vsh_am_ind+us98$vsh_cons+
                   us98$vsh_dem_npl+us98$vsh_dem_frm_lab+us98$vsh_green+
                   us98$vsh_ind_am+us98$vsh_libtn+us98$vsh_nat_law+
                   us98$vsh_ref+us98$vsh_rgt_life+us98$vsh_soc+
                   us98$vsh_us_taxp+us98$vsh_indep+us98$vsh_others)

us98.split <- findpatterns(us98)

fit98 <- list()
fit98[[1]] <- mvnmix(dat=us98.split[[1]],components=3,nrep=10,scatter=T)
fit98[[2]] <- mvnmix(dat=us98.split[[2]],components=3,nrep=10,scatter=T)
fit98[[3]] <- mvnmix(dat=us98.split[[3]],components=1,nrep=10,scatter=T)
fit98[[4]] <- mvnmix(dat=us98.split[[4]],components=3,nrep=10,scatter=T)

show.marginals(fit98,numdraws=50000)

sr98 <- swingratio(fit98,sims=10000)

##########
## 2000 ##
##########

us00 <- US.elections$'2000'

us00 <- data.frame(Total=us00$Total,
                   Democrat=us00$vsh_dem,
                   Republican=us00$vsh_rep,
                   
                   Other=us00$vsh_csce_cong+us00$vsh_cons+
                   us00$vsh_const+us00$vsh_dem_npl+us00$vsh_dem_frm_lab+
                   us00$vsh_dem_lib_un+us00$vsh_green+us00$vsh_indpdce+
                   us00$vsh_libtn+us00$vsh_nat_law+us00$vsh_pac_green+
                   us00$vsh_ref+us00$vsh_indep+us00$vsh_others)

us00.split <- findpatterns(us00)

fit00 <- list()
fit00[[1]] <- mvnmix(dat=us00.split[[1]],components=3,nrep=10,scatter=T)
fit00[[2]] <- mvnmix(dat=us00.split[[2]],components=2,nrep=10,scatter=T)
fit00[[3]] <- mvnmix(dat=us00.split[[3]],components=3,nrep=10,scatter=T)
fit00[[4]] <- mvnmix(dat=us00.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit00,numdraws=50000)

sr00 <- swingratio(fit00,sims=10000)

##########
## 2002 ##
##########

us02 <- US.elections$'2002'

us02 <- data.frame(Total=us02$Total,
                   Democrat=us02$vsh_dem,
                   Republican=us02$vsh_rep,
                   
                   Other=us02$vsh_cons+us02$vsh_const+us02$vsh_const_flor+
                   us02$vsh_dem_npl+us02$vsh_dem_frm_lab+us02$vsh_green+
                   us02$vsh_indpdce+us02$vsh_libtn+us02$vsh_un_cits+
                   us02$vsh_wisc_green+us02$vsh_work_fam+us02$vsh_indep+
                   us02$vsh_others)

us02.split <- findpatterns(us02)

fit02 <- list()
fit02[[1]] <- mvnmix(dat=us02.split[[1]],components=2,nrep=10,scatter=T)
fit02[[2]] <- mvnmix(dat=us02.split[[2]],components=3,nrep=10,scatter=T)
fit02[[3]] <- mvnmix(dat=us02.split[[3]],components=2,nrep=10,scatter=T)
fit02[[4]] <- mvnmix(dat=us02.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit02,numdraws=50000)

sr02 <- swingratio(fit02,sims=10000)

##########
## 2004 ##
##########

us04 <- US.elections$'2004'

us04 <- data.frame(Total=us04$Total,
                   Democrat=us04$vsh_dem,
                   Republican=us04$vsh_rep,
                   
                   Other=us04$vsh_cons+us04$vsh_const+us04$vsh_dem_frm_lab+
                   us04$vsh_green+us04$vsh_indpdce+us04$vsh_libtn+
                   us04$vsh_pce_just+us04$vsh_ref+us04$vsh_wisc_green+
                   us04$vsh_work_fam+us04$vsh_indep+us04$vsh_others)

us04.split <- findpatterns(us04)

fit04 <- list()
fit04[[1]] <- mvnmix(dat=us04.split[[1]],components=3,nrep=10,scatter=T)
fit04[[2]] <- mvnmix(dat=us04.split[[2]],components=3,nrep=10,scatter=T)
fit04[[3]] <- mvnmix(dat=us04.split[[3]],components=2,nrep=10,scatter=T)
fit04[[4]] <- mvnmix(dat=us04.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit04,numdraws=50000)

sr04 <- swingratio(fit04,sims=10000)

##########
## 2006 ##
##########

us06 <- US.elections$'2006'

us06 <- data.frame(Total=us06$Total,
                   Democrat=us06$vsh_dem,
                   Republican=us06$vsh_rep,
                   
                   Other=us06$vsh_am_ind+us06$vsh_cons+us06$vsh_const+
                   us06$vsh_dem_frm_lab+us06$vsh_green+us06$vsh_indpdce+
                   us06$vsh_lets_do+us06$vsh_libtn+us06$vsh_ref+
                   us06$vsh_soc_work+us06$vsh_work_fam+us06$vsh_indep+
                   us06$vsh_others)

us06.split <- findpatterns(us06)

fit06 <- list()
fit06[[1]] <- mvnmix(dat=us06.split[[1]],components=2,nrep=10,scatter=T)
fit06[[2]] <- mvnmix(dat=us06.split[[2]],components=3,nrep=10,scatter=T)
fit06[[3]] <- mvnmix(dat=us06.split[[3]],components=2,nrep=10,scatter=T)
fit06[[4]] <- mvnmix(dat=us06.split[[4]],components=4,nrep=10,scatter=T)

show.marginals(fit06,numdraws=50000)

sr06 <- swingratio(fit06,sims=10000)


# Assemble results (see Linzer's seatsvotes code).
USresults <- list(sr46,sr48,sr50,sr52,sr54,sr56,sr58,sr60,sr62,sr64,sr66,sr68,sr70,sr72,sr74,sr76,sr78,sr80,sr82,sr84,sr86,sr88,sr90,sr92,sr94,sr96,sr98,sr00,sr02,sr04,sr06)

swing <- NULL
swing.lm <- NULL
votes <- NULL
seats <- NULL
for (i in 1:length(USresults)) {
    swing <- rbind.fill(swing,as.data.frame(t(USresults[[i]]$swing)))
    swing.lm <- rbind.fill(swing.lm,as.data.frame(t(USresults[[i]]$swing.lm)))
    votes <- rbind.fill(votes,as.data.frame(t(USresults[[i]]$truevote)))
    seats <- rbind.fill(seats,as.data.frame(t(USresults[[i]]$trueseat)))
}

swing[is.na(swing)] <- swing.lm[is.na(swing)]

############################## Save Data #######################################

# set working directory
setwd("~/_data/sv_estimates")

# save objects to R image
save.image(file = "sv_estimates.RData")
